<?php
///////////////////////////Begin function generate form///////////////////////////////////////////////////////////////////
function Generate_HTML_Input(){
	global $_ISA_IMAGE_URL_PATH,$_ISA_LIB_URL_PATH,$_ISA_WEB_SITE_PATH;
	global $v_label, $v_type, $v_dataformat,$v_message,$v_optional,$v_xml_tag_in_db,$v_readonly_in_edit_mode,$v_disabled_in_edit_mode,$v_note,$v_relate_recordtype,$v_width,$v_row, $v_row_id, $v_max, $v_min, $v_maxlength,$v_tooltip;
	global $v_selectbox_option_sql,$v_selectbox_id_column, $v_selectbox_name_column;
	global $v_checkbox_multiple_sql,$v_checkbox_multiple_id_column,$v_checkbox_multiple_name_column ;
	global $v_js_function_list,	$v_js_action_list;
	global $v_value,$i,$v_row,$v_row_id,$v_status_dsp;
	$v_dataformat_str = Data_Format($v_dataformat);
	$v_url_path_calendar = '"'.$_ISA_LIB_URL_PATH.'isa-calendar/"';
	$v_optional_label = "";
	if ($v_optional=="false"){
		$v_optional_label = "<small class='normal_starmark'>*</small>";
	}
	if ($i==0){
		$v_str_label = $v_label.$v_optional_label."&nbsp;&nbsp;</td><td class='normal_label'>";
	}else{
		$v_str_label = "&nbsp;&nbsp;".$v_label.$v_optional_label."&nbsp;&nbsp;";
	}
	$v_checked = "";
	switch($v_type) {
		case "label";
			$v_ret_html = $v_label.$v_optional_label."&nbsp;&nbsp;";
			break;	
		case "relaterecord";		
			$v_ret_html = $v_str_label;
			$v_ret_html = $v_ret_html . "<input type='textbox' name='$v_xml_tag_in_db' class='normal_textbox' value='$v_value' title='$v_tooltip' style='width:$v_width' ".Property_Type("optional",$v_optional).Property_Type("readonly",$v_readonly_in_edit_mode).Property_Type("disabled",$v_disabled_in_edit_mode).Generate_Event($v_js_function_list, $v_js_action_list)." $v_dataformat_str xml_tag_in_db='$v_xml_tag_in_db' message='$v_message' onKeyDown='change_focus(document.forms(0),this)'>&nbsp;";
			$v_ret_html = $v_ret_html . "<input type='hidden' name='hdn_relate_record_code' value=''>";
			if ($v_value == ""){
				$v_ret_html = $v_ret_html . "<input type='button' name='btn_submit' style='width:auto' title='$v_tooltip' value='L&#7845;y th&#244;ng tin t&#7915; h&#7891; s&#417; li&#234;n quan' class='small_button' ".Property_Type("readonly",$v_readonly_in_edit_mode).Property_Type("disabled",$v_disabled_in_edit_mode)." onClick='show_modal_dialog_all_record_onclick(&quot;".$_ISA_WEB_SITE_PATH."record/archives/index.php&quot;,&quot;DISPLAY_ALL_RECORD_ARCHIVES&quot;,&quot;$v_relate_recordtype&quot;,document.forms(0).$v_xml_tag_in_db,document.forms(0).hdn_relate_record_code,&quot;DISPLAY_SINGLE_RECORD&quot;);'>";
				//echo htmlspecialchars($v_ret_html);
			}else{
				$arr_single_record_by_code = Query_Data_For_Colunm_name("Onegate_RecordGetSingleByCode '$v_value'");
				$v_record_id = $arr_single_record_by_code[0]['PK_RECORD'];
				$v_recordtype = $arr_single_record_by_code[0]['FK_RECORDTYPE'];
				if ($v_record_id>0 && $v_value!=""){
					$v_ret_html = $v_ret_html . "<a href='".$_ISA_WEB_SITE_PATH."record/archives/index.php?fuseaction=DISPLAY_SINGLE_RECORD_CONTENT&hdn_recordtype_filter=$v_recordtype&hdn_record_id=$v_record_id'>N&#7897;i dung c&#7911;a h&#7891; s&#417; li&#234;n quan</a>";
				}
			}
			break;
		case "textbox";
			$v_ret_html = $v_str_label;
			if ($v_dataformat == "isdate"){
				$v_ret_html = $v_ret_html . "<input type='textbox' name='$v_xml_tag_in_db' class='normal_textbox' value='$v_value' title='$v_tooltip' style='width:$v_width' ".Property_Type("optional",$v_optional).Property_Type("readonly",$v_readonly_in_edit_mode).Property_Type("disabled",$v_disabled_in_edit_mode).Generate_Event($v_js_function_list, $v_js_action_list)." $v_dataformat_str xml_tag_in_db='$v_xml_tag_in_db' message='$v_message' onKeyDown='change_focus(document.forms(0),this)'>";
				$v_ret_html = $v_ret_html . "<img src='". $_ISA_IMAGE_URL_PATH."calendar.gif' border='0' title='$v_tooltip' onclick='DoCal($v_url_path_calendar,document.forms(0).$v_xml_tag_in_db);' style='cursor:hand'>";
			}else{
				$v_ret_html = $v_ret_html . "<input type='textbox' name='$v_xml_tag_in_db' class='normal_textbox' value='$v_value' title='$v_tooltip' style='width:$v_width' ".Property_Type("optional",$v_optional).Property_Type("readonly",$v_readonly_in_edit_mode).Property_Type("disabled",$v_disabled_in_edit_mode).Generate_Event($v_js_function_list, $v_js_action_list)." $v_dataformat_str xml_tag_in_db='$v_xml_tag_in_db' message='$v_message' onKeyDown='change_focus(document.forms(0),this)'>";
				$v_ret_html = $v_ret_html . "";
			}
			$v_ret_html = $v_ret_html . $v_note;
			break;
		case "file";
			$v_ret_html = $v_str_label;
			$v_ret_html = $v_ret_html ."<input type='text' name='$v_xml_tag_in_db' class='normal_textbox'title='$v_tooltip' value='$v_value'  style='width:$v_width' style='border=0' readonly  $v_dataformat_str xml_tag_in_db='$v_xml_tag_in_db' message='$v_message' ".Property_Type("optional",$v_optional).Property_Type("readonly",$v_readonly_in_edit_mode).Property_Type("disabled",$v_disabled_in_edit_mode).">";
			$v_ret_html = $v_ret_html ."<br>";
			$v_ret_html = $v_ret_html ."<input type='file' name='txt_xml_file_name' class='normal_textbox' title='$v_tooltip' style='width:$v_width' onKeyDown='change_focus(document.forms(0),this)'".Generate_Event($v_js_function_list, $v_js_action_list).">";				
			$v_ret_html = $v_ret_html . "";
			$v_ret_html = $v_ret_html . $v_note;
			
			break;			
		case "is_file_attach";
			$arr_attach = $_SESSION['arr_attach'];
			$v_record = $_SESSION['v_record_id_session'];
			$v_ret_html = $v_str_label.'<br>';
			$v_ret_html = $v_ret_html . "<tr id = '$v_row_id' style='display:block'><td style='display:block'>&nbsp;";
			$v_ret_html = $v_ret_html . "</td><td colspan='10'>" . process_attach($arr_attach,$v_record);;
			$v_ret_html = $v_ret_html . "</td></tr>";
			break;			

		case "checkbox";
			if ($v_value=="true"){
				$v_checked = " checked ";
			}
			$v_ret_html = "<input type='checkbox' name='$v_xml_tag_in_db' class='normal_checkbox' title='$v_tooltip' $v_checked value='' ".Property_Type("optional",$v_optional).Property_Type("readonly",$v_readonly_in_edit_mode).Property_Type("disabled",$v_disabled_in_edit_mode).Generate_Event($v_js_function_list, $v_js_action_list)." xml_tag_in_db='$v_xml_tag_in_db' message='$v_message' onKeyDown='change_focus(document.forms(0),this)'>";
			$v_ret_html = $v_ret_html . "" . $v_label .$v_optional_label."";
			break;
		case "radio";
			if ($v_value=="true"){
				$v_checked = " checked ";
			}
			$v_ret_html =  "<input type='radio' name='$v_row_id' class='normal_checkbox' $v_checked value='' title='$v_tooltip' ".Property_Type("optional",$v_optional).Property_Type("readonly",$v_readonly_in_edit_mode).Property_Type("disabled",$v_disabled_in_edit_mode).Generate_Event($v_js_function_list, $v_js_action_list)." xml_tag_in_db='$v_xml_tag_in_db' message='$v_message' onKeyDown='change_focus(document.forms(0),this)'>";
			$v_ret_html = $v_ret_html . "" . $v_label .$v_optional_label."";
			break;
		case "textarea";
			$v_ret_html = $v_str_label;
			$v_ret_html = $v_ret_html . "<textarea class='normal_textarea' name='$v_xml_tag_in_db' rows='$v_row' title='$v_tooltip' value='$v_value' style='width:$v_width' ".Property_Type("optional",$v_optional).Property_Type("readonly",$v_readonly_in_edit_mode).Property_Type("disabled",$v_disabled_in_edit_mode).Generate_Event($v_js_function_list, $v_js_action_list)." xml_tag_in_db='$v_xml_tag_in_db' message='$v_message'>$v_value</textarea>";
			break;
		case "selectbox";
			$v_ret_html = $v_str_label;
			$v_ret_html = $v_ret_html . "<select class='normal_selectbox' name='$v_xml_tag_in_db' title='$v_tooltip' style='width:$v_width' ".Property_Type("optional",$v_optional).Property_Type("readonly",$v_readonly_in_edit_mode).Property_Type("disabled",$v_disabled_in_edit_mode).Generate_Event($v_js_function_list, $v_js_action_list)." xml_tag_in_db='$v_xml_tag_in_db' message='$v_message' onKeyDown='change_focus(document.forms(0),this)'>";
			$v_ret_html = $v_ret_html . "<option id='' value=''>--Ch&#7885;n $v_label--</option>"._generate_select_option(Query_Data($v_selectbox_option_sql),$v_selectbox_id_column,$v_selectbox_id_column,$v_selectbox_name_column,$v_value);
			$v_ret_html = $v_ret_html . "</select>";
			break;
		case "checkboxmultiple";
			$v_ret_html = $v_label.$v_optional_label;
			$v_ret_html = $v_ret_html . "<tr id = '$v_row_id' style='display:block'><td style='display:none'><input type='textbox' name='$v_xml_tag_in_db' value='' readonly ".Property_Type("optional",$v_optional)." xml_tag_in_db='$v_xml_tag_in_db' message='$v_message'>";
			$v_ret_html = $v_ret_html . "</td><td colspan='10'>".Generate_Checkbox_Multiple(Query_Data($v_checkbox_multiple_sql),$v_checkbox_multiple_id_column,$v_checkbox_multiple_name_column,$v_value);
			$v_ret_html = $v_ret_html . "</td></tr>";
			break;
		case "button";
			$v_ret_html = $v_ret_html . "<input type='button' name='$v_xml_tag_in_db' class='small_button' value='$v_label' title='$v_tooltip' style='width:$v_width' ".Property_Type("optional",$v_optional).Property_Type("readonly",$v_readonly_in_edit_mode).Property_Type("disabled",$v_disabled_in_edit_mode).Generate_Event($v_js_function_list, $v_js_action_list)." message='$v_message' onKeyDown='change_focus(document.forms(0),this)'>";
			$v_ret_html = $v_ret_html . $v_note;
			break;			
		default:
			$v_ret_html = "";
	}	
	return $v_ret_html; 
}
////////////////////////
function process_attach($p_arr,$v_id_r){
	$v_record_id = $v_id_r;
	$v_goto_url_for_delete_file = "javascript:delete_row(document.all.tr_line_new,document.forms(0).chk_file_attach_new_id,document.forms(0).hdn_deleted_new_file_id_list);";
	$v_goto_url_for_add_file = "javascript:add_row(document.all.tr_line_new," . _CONST_ARTICLE_UPDATE_DATE_MAXLENGTH .");";
	
	$strHTML = $strHTML . "<table class='list_table2'  width='98%' cellpadding='0' cellspacing='0'><col width='5%'><col width='95%'>";	
	if(isset($p_arr)){
		$v_count_file = sizeof($p_arr);
	}else{
		$v_count_file = 0;
	}	
	if (($v_record_id>0)&&($v_count_file>0)) {
		// Goi thu tuc xu ly khi xoa cac file da co
		$v_goto_url_for_delete_file = $v_goto_url_for_delete_file . "delete_row(document.all.tr_line_exist,document.forms(0).chk_file_attach_exist_id,document.forms(0).hdn_deleted_exist_file_id_list);";
		for ($i = 0; $i<$v_count_file; $i++) {
			$v_file_id = $p_arr[$i]['PK_RECORD_FILE'];
			$v_file_name = $p_arr[$i]['C_FILE_NAME']; 
			if(_is_sqlserver()){
				$v_file_url = trim(_CONST_UPLOAD_IMAGE_URL_PATH_VIEW). $v_file_name;
				$v_goto_url = "javascript:filename_onclick(&quot;T_ONEGATE_RECORD_FILE&quot;,&quot;PK_RECORD_FILE&quot;,&quot;C_FILE_NAME&quot;,&quot;C_FILE_CONTENT&quot;," . strval($v_file_id) . ",&quot;".$v_file_url."&quot;);" ;
				$target = "";
			}
			$strHTML = $strHTML . "<tr id='tr_line_exist'><td colspan='2'><input type='checkbox' name='chk_file_attach_exist_id' value=$v_file_id><a href='$v_goto_url' class='normal_link' $target>$v_file_name</a></td>";
			$strHTML = $strHTML . "</tr>";	
		}
	}
//Vong lap hien thi cac file dinh kem se them vao van ban
	$v_add = _CONST_ADD_BUTTON;
	$v_del = _CONST_DELETE_BUTTON;
	for($i=0; $i<_CONST_MAX_NUMBER_ATTACH_FILE; $i++){					
		if ($i<=1 ) { //and $v_is_granted_update
			$v_str_show="block";
		}else{
			$v_str_show="none";
		}
		$strHTML = $strHTML . "<tr id='tr_line_new' style='display:$v_str_show'><td><input type='checkbox' name='chk_file_attach_new_id' value=$i></td>";
		$strHTML = $strHTML . "<td><input type='file' name='file_attach$i' size='70' class='small_textbox' optional='true'></td></tr>";
	}	
	$strHTML = $strHTML . "<tr align='center'><td colspan='2'><a href='$v_goto_url_for_add_file' class='small_link'>$v_add</a>&nbsp;";
	$strHTML = $strHTML . "	<a href='$v_goto_url_for_delete_file' class='small_link'>$v_del</a></td></tr>";
	$strHTML = $strHTML . "</table>";
	//echo htmlspecialchars($strHTML);//exit;
	return $strHTML;
}

function Generate_HTML_Output(){
	global $v_label, $v_type, $v_dataformat,$v_width,$v_row, $v_row_id;
	global $v_selectbox_option_sql,$v_selectbox_id_column, $v_selectbox_name_column;
	global $v_value,$i,$v_row ;
	$v_dataformat_str = Data_Format($v_dataformat);
	$v_optional_label = "";
	$v_str_label = "&nbsp;&nbsp;".$v_label."&nbsp;&nbsp;";
	switch($v_type) {
		case "label";
			$v_ret_html = $v_label.$v_optional_label."&nbsp;&nbsp;";
			break;	
		case "textbox";
			$v_ret_html = $v_str_label;
			$v_ret_html = $v_ret_html . $v_value;
			break;
		case "selectbox";
			$v_ret_html = $v_str_label;
			$v_ret_html = $v_ret_html .get_text_from_array(Query_Data($v_selectbox_option_sql),$v_selectbox_id_column,$v_selectbox_id_column,$v_selectbox_name_column,$v_value);
			break;
		default:
			$v_ret_html = "";
	}	
	return $v_ret_html;
}
//Chuc nang: Doi so thanh chu
//Ham nay chi doi so thanh chu trong pham vi tu 1->99
/////////////////////////////////////////////////
function convert_number_to_chars($v_number){
	$arr_string[0] = "m&#432;&#417;i";
	$arr_string[1] = "m&#7897;t";
	$arr_string[2] = "hai";
	$arr_string[3] = "ba";
	$arr_string[4] = "b&#7889;n";
	$arr_string[5] = "n&#259;m";
	$arr_string[6] = "s&#225;u";
	$arr_string[7] = "b&#7843;y";
	$arr_string[8] = "t&#225;m";
	$arr_string[9] = "ch&#237;n";
	$arr_string[10] = "m&#432;&#7901;i";
	if(($v_number < 10) && ($v_number > 0)){
		if($v_number == 1){
			return "nh&#7845;t";
		}else{
			return $arr_string[$v_number];
		}	
	}else{
		if(($v_number >= 10) && ($v_number <= 99)){
			$v_tens = substr($v_number,0,1);
			$v_less_tens = substr($v_number,1,1);
			if($v_number == 10){
				return $arr_string[$v_number];
			}else{
				if($v_number % 10 == 0){
					return $arr_string[$v_tens] . "  " . $arr_string[0];
				}else
				if($v_number < 20){
					return $arr_string[0]  . "  " . $arr_string[$v_less_tens];
				}else{
					return $arr_string[$v_tens]  . "  " . $arr_string[$v_less_tens];
				}	
			}	
		}
	}
	return "";
}
/////////////////////////////////////////////////
function Get_text_from_array($arr_list,$IdColumn,$ValueColumn,$NameColumn,$SelectedValue) {
	$strHTML = "";
	$count=sizeof($arr_list);
	//var_dump($arr_list);
	for($row_index = 0;$row_index< $count;$row_index++){
		$strID=trim($arr_list[$row_index][$IdColumn]);
		$strValue=trim($arr_list[$row_index][$ValueColumn]);
		$DspColumn=trim($arr_list[$row_index][$NameColumn]);
		if($strID == $SelectedValue) {
			$strHTML = $DspColumn;
		} 
	}
	return $strHTML;
}
function Generate_Checkbox_Multiple($arr_list,$IdColumn,$NameColumn,$Valuelist) {
$v_record = $_SESSION['v_record_id_session'];
//echo 'ffff'.$v_record;
	global $v_xml_tag_in_db,$v_label,$v_tooltip;
	global $v_status_dsp;
	//echo 'hung'.$v_status_dsp.'<br>';
	$arr_value = explode(",", $Valuelist);
	$v_count_item = sizeof($arr_list);
	$v_count_value = sizeof($arr_value);
	$v_tr_name = '"tr_'.$v_xml_tag_in_db.'"';
	$v_radio_name = '"rad_'.$v_xml_tag_in_db.'"';
	$strHTML = "<DIV title='$v_tooltip' STYLE='overflow: auto; height:105pt;padding-left:5px;margin:0px'>";
	$strHTML = $strHTML . "<table class='list_table2'  width='100%' cellpadding='0' cellspacing='0'><col width='2%'><col width='98%'>";
	if ($v_count_item > 0){
		$i=0;
		$v_item_url_onclick = "_change_item_checked(this,$v_tr_name,$v_radio_name)";
		while ($i<$v_count_item) {
			$v_item_id = $arr_list[$i][$IdColumn];
			$v_item_name = $arr_list[$i][$NameColumn]; 
			if ($v_current_style_name == "odd_row"){
				$v_current_style_name = "round_row";
			}else{
				$v_current_style_name = "odd_row";
			}
			$v_item_checked = "";
			$v_item_display = "block";
			if ($Valuelist!=""){ //Kiem tra xem Hieu chinh hay la them moi
				$v_item_display = "none";
			}
			for ($j=0; $j<$v_count_value; $j++)
			if ($arr_value[$j]==$v_item_id){
				$v_item_checked = "checked";
				$v_item_display = "block";
				break;
			}
			$strHTML = $strHTML . "<tr id=$v_tr_name  value='$v_item_id' checked='$v_item_checked' class='$v_current_style_name' style='display:$v_item_display'>";
			$strHTML = $strHTML . "<td><input type='checkbox' name='chk_item_id' value='$v_item_id' label = '$v_item_name' xml_tag_in_db_name ='$v_xml_tag_in_db' $v_item_checked  onClick='$v_item_url_onclick' onKeyDown='change_focus(document.forms(0),this)'></td>";
			$strHTML = $strHTML . "<td style='width:100%'>$v_item_name</td></tr>";
			$i++;	
		}	
			$v_item_display_doc_another = "block";
			$v_item_checked_doc_another = "";
			if($v_status_dsp > 0){
				$v_sql = "Select dbo.f_GetValueOfXMLtag(C_RECEIVED_RECORD_XML_DATA,'doc_another') AS C_DOC_ANOTHER from T_ONEGATE_RECORD where PK_RECORD = $v_record";
				$arr_single_data = Query_Data($v_sql);
				$v_doc = trim($arr_single_data[0][0]);
				if(($v_doc<>"") && (!is_null($v_doc))){
					$v_item_display_doc_another = "block";
					$v_item_checked_doc_another = "checked";
				}	
				$strHTML = $strHTML . "<tr id=$v_tr_name  value='$v_item_id' class='$v_current_style_name' style='display:$v_item_display_doc_another'>";
				$strHTML = $strHTML . "<td><input type='checkbox' name='chk_item_id' value='TAI_LIEU_KHAC' label = 'T&#224;i li&#7879;u kh&#225;c' xml_tag_in_db_name ='$v_xml_tag_in_db' $v_item_checked_doc_another  onClick='show_hide_content_doc_another();$v_item_url_onclick' onKeyDown='change_focus(document.forms(0),this)'></td>";
				$strHTML = $strHTML . "<td style='width:100%'>T&#224;i li&#7879;u kh&#225;c</td></tr>";		
			}	
	}
	if ($Valuelist!=""){   //Kiem tra xem Hieu chinh hay la them moi
		$v_checked_show_row_all = "";
		$v_checked_show_row_selected = "checked";
	}else{
		$v_checked_show_row_all = "checked";
		$v_checked_show_row_selected = "";
	}
	if ($v_label==""){
		$v_label = "&#273;&#7889;i t&#432;&#7907;ng";
	}else{
		$v_label = FirstStringToLower($v_label);
	}
	$strHTML = $strHTML ."</table>";
	$strHTML = $strHTML . "</DIV>";
	$strHTML = $strHTML . "<table width='100%' cellpadding='0' cellspacing='0'><col width='2%'><col width='98%'>";

	$strHTML = $strHTML . "<tr><td class='small_radiobutton' colspan='10' align='right'>";
	$strHTML = $strHTML . "<input type='radio' id=$v_radio_name value='1' $v_checked_show_row_all onClick='_show_row_all($v_radio_name,$v_tr_name)' onKeyDown='change_focus(document.forms(0),this)'>Hi&#7875;n th&#7883; t&#7845;t c&#7843; $v_label";
	$strHTML = $strHTML . "<input type='radio' id=$v_radio_name value='2' $v_checked_show_row_selected onClick='_show_row_selected($v_radio_name,$v_tr_name)' onKeyDown='change_focus(document.forms(0),this)'>Ch&#7881; hi&#7875;n th&#7883; c&#225;c $v_label &#273;&#432;&#7907;c ch&#7885;n";
	$strHTML = $strHTML . "</td></tr>";
	$strHTML = $strHTML ."</table>";
	//echo htmlspecialchars($strHTML).'<br><br><br><br><br><br>';
	return $strHTML;
}
function Generate_Event($v_js_function_list, $v_js_action_list){
	$arr_js_function_list = explode(",", $v_js_function_list);
	$arr_js_action_list = explode(",", $v_js_action_list);
	$v_count_function = sizeof($arr_js_function_list);
	$v_count_action = sizeof($arr_js_action_list);
	$v_count = $v_count_function > $v_count_action ? $v_count_action : $v_count_function;
	$v_temp = "";
	for ($i=0;$i<$v_count;$i++){
		$v_temp = $v_temp . " $arr_js_action_list[$i]='$arr_js_function_list[$i]' ";  
	}
	return $v_temp;
}
function Property_Type($v_type, $v_value){
	switch($v_type) {	
		case "optional";
			if ($v_value=="false"){
				$v_ret_html = "";
			}else{
				$v_ret_html = " optional = true ";
			}
			break;
		case "readonly";
			if ($v_value=="false"){
				$v_ret_html = "";
			}else{
				$v_ret_html = " readonly = true ";
			}
			break;
		case "disabled";
			if ($v_value=="false"){
				$v_ret_html = " ";
			}else{
				$v_ret_html = " disabled = true ";
			}
			break;
		default:
			$v_ret_html = "";
	}	
	return $v_ret_html;

}
function Data_Format($v_dataformat){
	switch($v_dataformat) {	
		case "isemail";
			$v_ret_html = " isemail=true " ;
			break;
		case "isdate";
			$v_ret_html = " isdate=true " ;
			break;
		case "isnumeric";
			$v_ret_html = " isnumeric=true " ;
			break;
		case "ismoney";
			$v_ret_html = " isnumeric=true onKeyUp='format_money(this)' ";
			break;
		default:
			$v_ret_html = "";
	}	
	return $v_ret_html;
}
//Lay du lieu dua vao cau lenh select
function Query_Data($v_str_sql){
	global $ado_conn;
	if(_is_sqlserver()){
		$ado_conn->SetFetchMode(ADODB_FETCH_NUM);
		$arr_all_data = $ado_conn->GetAll($v_str_sql); 
	}
//	var_dump($arr_all_data);
	return $arr_all_data;
}
//Lay du lieu dua vao cau lenh select
function Query_Data_For_Colunm_name($v_str_sql){
	global $ado_conn;
	if(_is_sqlserver()){
		$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
		$arr_all_data = $ado_conn->GetAll($v_str_sql); 
	}
//	var_dump($arr_all_data);
	return $arr_all_data;
}
function Element_Type($v_type){
	global $v_value,$v_url,$v_align,$v_inc,$v_selectbox_option_sql,$v_php_function,$row_index;
	switch($v_type) {	
		case "checkbox";
			$v_ret_html = '<td align="'.$v_align.'"><input type="checkbox" name="chk_item_id" value="'.$v_value.'">&nbsp;<a name="'.$v_value.'">&nbsp;</a></td>';
			break;
		case "selectbox";
			$v_ret_html = '<td align="'.$v_align.'" onclick="'.$v_url.'">'.get_text_from_array(Query_Data($v_selectbox_option_sql),"0","0","1",$v_value).'&nbsp;</td>';
			break;
		case "function";
			$v_ret_html = '<td align="'.$v_align.'" onclick="'.$v_url.'">'.call_user_func($v_php_function,$v_value).'&nbsp;</td>';
			break;
		case "date";
			$v_ret_html = '<td align="'.$v_align.'" onclick="'.$v_url.'">'._yyyymmdd_to_ddmmyyyy($v_value).'&nbsp;</td>';
			break;
		case "time";
			$v_ret_html = '<td align="'.$v_align.'" onclick="'.$v_url.'">'._yyyymmdd_to_hhmmss($v_value).'&nbsp;</td>';
			break;
		case "text";
			$v_ret_html = '<td align="'.$v_align.'" onclick="'.$v_url.'">'.$v_value.'&nbsp;</td>';
			break;
		case "identity";
			$v_ret_html = '<td align="'.$v_align.'" onclick="'.$v_url.'">'.$v_inc.'&nbsp;</td>';
			break;
		case "money";
			$v_ret_html = '<td align="'.$v_align.'" onclick="'.$v_url.'">'.onegate_data_format($v_value).'&nbsp;</td>';
			break;
		//bui dong theo dung nhu kieu nguoi su dung nhap trong textarea
		/*
			Vi du: Trong textarea NSD danh
					Giay khai sinh
					bang tot nghiep 
					- thi khi hien thi ra man hinh no lai hien thi nhu sau
					Giay khai sinh bang tot nghiep
					Kieu isbreakcontent se be dung theo NSD da nhap
		*/
		case "isbreakcontent";
			$v_len = strlen($v_value);
			if($v_len > 0){			
				for($index = 0;$index < $v_len;$index++){
					if(ord(substr($v_value,$index,1)) == 10){//=10 la ma xuong dau dong
						$v_value = str_replace(chr(10),"<br>",$v_value);
					}
				}	
			}
			$v_ret_html = '<td align="'.$v_align.'" onclick="'.$v_url.'">'.$v_value.'&nbsp;</td>';
			break;			
		default:
			$v_ret_html = $v_value;
	}	
	return $v_ret_html;
}
function Report_Element_Type($v_type){
	global $v_value,$v_url,$v_align,$v_inc,$v_selectbox_option_sql,$v_php_function;
	switch($v_type) {	
		case "checkbox";
			$v_ret_html = '<td class="data" align="'.$v_align.'"><input type="checkbox" name="chk_item_id" value="'.$v_value.'">&nbsp;<a name="'.$v_value.'">&nbsp;</a></td>';
			break;
		case "selectbox";
			$v_ret_html = '<td class="data" align="'.$v_align.'" onclick="'.$v_url.'">'.get_text_from_array(Query_Data($v_selectbox_option_sql),"0","0","1",$v_value).'&nbsp;</td>';
			break;
		case "function";
			$v_ret_html = '<td class="data" align="'.$v_align.'" onclick="'.$v_url.'">'.call_user_func($v_php_function,$v_value).'&nbsp;</td>';
			break;
		case "date";
			$v_ret_html = '<td class="data" align="'.$v_align.'" onclick="'.$v_url.'">'._yyyymmdd_to_ddmmyyyy($v_value).'&nbsp;</td>';
			break;
		case "time";
			$v_ret_html = '<td class="data" align="'.$v_align.'" onclick="'.$v_url.'">'._yyyymmdd_to_hhmmss($v_value).'&nbsp;</td>';
			break;
		case "text";
			$v_ret_html = '<td class="data" align="'.$v_align.'" onclick="'.$v_url.'">'.$v_value.'&nbsp;</td>';
			break;
		case "isbreak_content_for_report";
			$v_len = strlen($v_value);
			if($v_len > 0){			
				for($index = 0;$index < $v_len;$index++){
					if(ord(substr($v_value,$index,1)) == 10){//=10 la ma xuong dau dong
						$v_value = str_replace(chr(10),"<br>",$v_value);
					}
				}	
			}			
			$v_ret_html = '<td class="data" align="'.$v_align.'" onclick="'.$v_url.'">'.$v_value.'&nbsp;</td>';
			break;			
		case "identity";
			$v_ret_html = '<td class="data" align="'.$v_align.'" onclick="'.$v_url.'">'.$v_inc.'&nbsp;</td>';
			break;
		case "money";
			$v_ret_html = '<td class="data" align="'.$v_align.'" onclick="'.$v_url.'">'.onegate_data_format($v_value).'&nbsp;</td>';
			break;
		default:
			$v_ret_html = $v_value;
	}	
	return $v_ret_html;
}
/////////////////////////End function generate form///////////////////////////////////////////////////////////////
//Ham chuyen thoi gian dang ddmmyyyyhhmmss sang dang yyyymmddhhmmss (de cap nhat vao co so du lieu)
function ddmmyyyyhhmmss_to_yyyymmddhhmmss($p_ddmmyyyyhhmmss) {
	if (strlen($p_ddmmyyyyhhmmss)==0 || is_null($p_ddmmyyyyhhmmss)) {
		$date = "";
		return $date;
	}
	$temp_date = $p_ddmmyyyyhhmmss;
	$temp_date = str_replace('/',' ',$temp_date);
	$temp_date = str_replace('-',' ',$temp_date);
	$date = explode(" ",$temp_date);
	if (strlen($date[2])==2) {
		$date_temp = "20".$date[2] . "-" .  $date[1] ."-" . $date[0] ." ". $date[3];
	} else {
		$date_temp = $date[2] . "-" .  $date[1] ."-" . $date[0] ." ". $date[3];
	} 
	return $date_temp; 
}
//Ham chuyen thoi gian dang yyyymmddhhmmss sang dang yyyymmddhhmmss (de hien thi ra man hinh)
function yyyymmddhhmmss_to_ddmmyyyyhhmmss($p_ddmmyyyyhhmmss) {
	if (strlen($p_ddmmyyyyhhmmss)==0 || is_null($p_ddmmyyyyhhmmss)) {
		$date = "";
		return $date;
	}
	$temp_date = $p_ddmmyyyyhhmmss;
	$temp_date = str_replace('/',' ',$temp_date);
	$temp_date = str_replace('-',' ',$temp_date);
	$date = explode(" ",$temp_date);
	$date_temp = $date[2] . "/" .  $date[1] ."/" . $date[0] ." ". $date[3];
	return $date_temp; 
}
function Rich_Text_Format($v_string){
	$v_string = str_replace("&#","\u",$v_string);
	$v_string = str_replace(";","\'3f",$v_string);
	$v_string = "{".$v_string."}";
	return $v_string;
}
function Print_Receipt_From_XML($v_xml_format,$v_richtext_format,$v_xml_replace_input,$arr_record){
//echo htmlspecialchars($v_xml_replace_input); exit;
	global $_ISA_OWNER_NAME; 
	$v_richtext_ouput = $v_richtext_format;
	$table_struct_rax = new RAX(); 
	$table_struct_rec = new RAX(); 
	$table_struct_rax->open($v_xml_format);
	$table_struct_rax->record_delim = 'replace';
	$table_struct_rax->parse();
	$table_struct_rec = $table_struct_rax->readRecord(); 
	while ($table_struct_rec) { 
		$table_struct_row = $table_struct_rec->getRow();
		$v_from_xml_data = $table_struct_row["from_xml_data"];
		$v_data_format = $table_struct_row["data_format"];
		$v_upcase = $table_struct_row["upcase"];
		$v_find_string = $table_struct_row["find_string"];
		$v_field_name = $table_struct_row["field_name"];
		$v_xml_tag_in_db = $table_struct_row["xml_tag_in_db"];
		$v_sql = "";
		if ($v_data_format=="selectbox"){
			$v_sql = $table_struct_row["selectbox_option_sql"];
		}
		if ($v_data_format=="checkboxmultiple"){
			$v_sql = $table_struct_row["checkbox_multiple_sql"];
		}
		if ($v_data_format=="iscountdocument"){
			$v_sql = $table_struct_row["sql_count_document"];
		}		
		
		if ($v_from_xml_data=="true"){
			if ($v_xml_replace_input!=""){
				$column_rax = new RAX(); 
				$column_rec = new RAX();
				$column_rax->open($v_xml_replace_input);
				$column_rax->record_delim = 'data_list';
				$column_rax->parse();
				$column_rec = $column_rax->readRecord(); 
				$column_row = $column_rec->getRow();
				$v_xml_tag_in_db_value = _restore_XML_bad_char($column_row[$v_xml_tag_in_db]); 
				if($v_data_format == "isdate"){
					//if(trim($v_xml_tag_in_db_value)!='' && !is_null($v_xml_tag_in_db_value))
						$v_xml_tag_in_db_value = _ddmmyyyy_to_yyyymmdd($v_xml_tag_in_db_value);
					//else
						//$v_xml_tag_in_db_value = " nsg&#224;y97695,6,76 ... th&#225;ng ... n&#259;m ....";
				}
				if($v_data_format == "is_license_number"){
					$v_recordtype_filter_code = $_SESSION['v_recordtype_filter_code'];							
					$v_xml_tag_in_db_value = get_date_by_license_number($v_xml_tag_in_db_value,$v_recordtype_filter_code);
					if((trim($v_xml_tag_in_db_value)<>"") && !is_null($v_xml_tag_in_db_value)){
						$v_xml_tag_in_db_value = _ddmmyyyy_to_yyyymmdd($v_xml_tag_in_db_value);
						$v_xml_tag_in_db_value = "nsg&#224;y ".date("d", strtotime($v_xml_tag_in_db_value))." th&#225;ng ".date("m", strtotime($v_xml_tag_in_db_value))." n&#259;m ".date("Y", strtotime($v_xml_tag_in_db_value));
					}
				}
				if($v_data_format == "isnumbertostring"){
					$v_xml_tag_in_db_value = convert_number_to_chars($v_xml_tag_in_db_value);
				}
				if($v_data_format == "ismoney_convert"){
					$v_xml_tag_in_db_value = trim($v_xml_tag_in_db_value);
					//echo 'v_xml_tag_in_db_value'.$v_xml_tag_in_db_value; exit;
				}				
				$v_xml_tag_in_db_value = str_replace("\n" ,"<br>",$v_xml_tag_in_db_value);
			}
			$v_replace_string = view_data_format($v_data_format,$v_xml_tag_in_db_value,$v_sql,$_ISA_OWNER_NAME);
		}else{
			$v_field_name_value = $arr_record[$v_field_name];
			$v_replace_string = view_data_format($v_data_format,$v_field_name_value,$v_sql,$_ISA_OWNER_NAME);
		}
		$v_richtext_ouput = str_replace($v_find_string,$v_replace_string,$v_richtext_ouput);
		$table_struct_rec = $table_struct_rax->readRecord();
	}
	return $v_richtext_ouput;
}
/*function Rich_Text_Format($v_string){
	$v_string = str_replace("&#","\u",$v_string);
	$v_string = "{".$v_string."}";
	return $v_string;
}*/
////////////////////////////////////////////////////////////////////////////
function get_date_by_license_number($v_xml_tag_in_db_value,$v_recordtype_filter_code){
	$v_value = $v_xml_tag_in_db_value;
	$v_sql = "select dbo.f_GetValueOfXMLtag(C_LICENSE_XML_DATA, 'record_number') AS LICENSE_NUMBER";
	$v_sql = $v_sql . ",dbo.f_GetValueOfXMLtag(C_LICENSE_XML_DATA, 'record_quanlity') AS RECORD_QUANLITY";
	$v_sql = $v_sql . ",dbo.f_GetValueOfXMLtag(C_LICENSE_XML_DATA, 'record_date') AS RECORD_DATE";
	$v_sql = $v_sql . " from T_onegate_record where fk_recordtype = " . "'" . $v_recordtype_filter_code . "'" . " and dbo.f_GetValueOfXMLtag(C_LICENSE_XML_DATA, 'record_number') = " . "'" . $v_value . "'";
	$v_sql = $v_sql . " and dbo.f_GetValueOfXMLtag(C_LICENSE_XML_DATA, 'record_quanlity') = ''";
	$v_arr = Query_Data_For_Colunm_name($v_sql);
	return $v_arr[0]['RECORD_DATE'];
}
////////////////////////////////////////////////////////////////////////////
function view_data_format($v_dataformat,$v_valueformat,$v_sql,$v_unit_name){
	global $_ISA_UNIT_NAME;
	switch($v_dataformat) {	
		case "text";
			$v_ret_html = $v_valueformat ;
			break;
		case "getdate";
			$v_ret_html = "ng&#224;y ".date("d")." th&#225;ng ".date("m")." n&#259;m ".date("Y");
			break;
		case "unit";
			$v_ret_html = $v_unit_name;
			break;
		case "istitle";
			$v_ret_html = $_ISA_UNIT_NAME;
			break;
		case "isdate";
			if(trim($v_valueformat) == ""){				
				$v_ret_html = " ng&#224;y ... th&#225;ng ... n&#259;m ".substr(date("Y"),0,3)."...";
			}else{
				$v_ret_html = "ng&#224;y ".date("d", strtotime($v_valueformat))." th&#225;ng ".date("m", strtotime($v_valueformat))." n&#259;m ".date("Y", strtotime($v_valueformat));
			}
			break;
		case "date";
			if(trim($v_valueformat) == ""){
				$_ret_html = "";
			}else{
				$v_ret_html = date("d", strtotime($v_valueformat))."/".date("m", strtotime($v_valueformat))."/".date("Y", strtotime($v_valueformat));
			}
			break;
		case "ismoney";
			$v_ret_html = onegate_data_format($v_valueformat);
			break;
		case "ismoney_convert";			
			if((trim($v_valueformat)<>"") && (!is_null(trim($v_valueformat)))){
				$v_ret_html = str_replace(',','.',$v_valueformat);
			}			
			break;
		case "selectbox";
			$v_ret_html = get_text_from_array(Query_Data($v_sql),"0","0","1",$v_valueformat);
			break;
		case "checkboxmultiple";
			$v_ret_html = "";
			if ($v_valueformat != ""){
				$arr_all_data = Query_Data($v_sql);
				$v_valueformat = str_replace(",TAI_LIEU_KHAC","",$v_valueformat);
				$arr_valueformat= explode(",", $v_valueformat);
				for ($i=0;$i<sizeof($arr_valueformat);$i++){
					$v_ret_html = $v_ret_html . " ".($i+1).". " .get_text_from_array($arr_all_data,"0","0","1",$arr_valueformat[$i]);
					if ($i != sizeof($arr_valueformat)-1){
						$v_ret_html = $v_ret_html . "<BR>";
					}
				}
			}
			//exit;
			break;
		//kieu dem tai lieu	
		case "iscountdocument";	
				$v_record_id_temp = $_SESSION['v_record_id_print'];
				$v_sql = str_replace('#id_record#', $v_record_id_temp, $v_sql);
				$arr_all_data = Query_Data($v_sql);
				$count_document = $arr_all_data[0][0];
				$v_ret_html = $arr_all_data[0][0];
			break;	
		//dinh dang du lieu khi in: Neu NSD nhap so tien tam thu thi so tin + dong. Nguoc lai hien thi o dong	
		case "ismoneyprovisionally";
			$v_valueformat = trim($v_valueformat);
			if(($v_valueformat=="")){
				$v_ret_html = "0 &#273;&#7891;ng";
			}else{
				$v_ret_html = str_replace(",",".",$v_valueformat) . " &#273;&#7891;ng";
			}
			break;					
		//
		case "isrecordnumber";
			$v_valueformat = trim($v_valueformat);
			$v_valueformat = str_replace(".","/",$v_valueformat);
			if(($v_valueformat<>"")){
				$arr_string = split("/",$v_valueformat);
				if(count($arr_string) > 0){
					$v_ret_html = substr($v_valueformat,0,strlen($v_valueformat)-strlen($arr_string[2])) . "GBN";
				}	
			}
			break;					
		default:
			$v_ret_html = $v_valueformat;
	}	
	return $v_ret_html;
}
function get_value_from_two_list($v_recordtype_code,$v_recordtype_code_list,$v_xml_tag_in_db_list){
	$arr_xml_tag_in_db_list = explode(",", $v_xml_tag_in_db_list);
	$arr_recordtype_code_list = explode(",", $v_recordtype_code_list);
	for($i=0;$i < sizeof($arr_xml_tag_in_db_list); $i++){
		if ($arr_recordtype_code_list[$i]==$v_recordtype_code){
			return $arr_xml_tag_in_db_list[$i];
		}
	}
	return $arr_xml_tag_in_db_list[0];
}
function replace_tag_xml_value_in_sql($v_sql_replace,$v_xml_file,$v_str_xml_data){
	//echo htmlspecialchars($v_str_xml_data);exit;
	$v_sql_replace_temp = $v_sql_replace;
	$v_xml_file_temp = $v_xml_file;
	$v_table_struct_rax = new RAX(); 
	$v_table_struct_rec = new RAX(); 
	$v_table_struct_rax->open($v_xml_file);
	$v_table_struct_rax->record_delim = 'row';
	$v_table_struct_rax->parse();
	$v_table_struct_rec = $v_table_struct_rax->readRecord(); 
	while ($v_table_struct_rec) { 
		$v_table_struct_row = $v_table_struct_rec->getRow();
		$v_tag_list = $v_table_struct_row["tag_list"];
		$arr_tag = explode(",", $v_tag_list);
		for($i=0;$i < sizeof($arr_tag); $i++){
			$v_formfield_rax = new RAX(); 
			$v_formfield_rec = new RAX(); 
			$v_formfield_rax->open($v_xml_file_temp);
			$v_formfield_rax->record_delim = $arr_tag[$i];
			$v_formfield_rax->parse();
			$v_formfield_rec = $v_formfield_rax->readRecord(); 			
			$v_formfield_row = $v_formfield_rec->getRow(); 
			$v_data_format = $v_formfield_row["data_format"];
			$v_xml_data = $v_formfield_row["xml_data"];
			$v_xml_tag_in_db = $v_formfield_row["xml_tag_in_db"];
			
			$v_colume_name = $v_formfield_row["colume_name"];
			$v_compare_operator = _restore_XML_bad_char($v_formfield_row["compare_operator"]);
			if ($v_str_xml_data!=""){
				$v_column_rax = new RAX(); 
				$v_column_rec = new RAX();
				$v_column_rax->open($v_str_xml_data);
				$v_column_rax->record_delim = 'data_list';
				$v_column_rax->parse();
				$v_column_rec = $v_column_rax->readRecord(); 
				$v_column_row = $v_column_rec->getRow();
				$v_value_input = $v_column_row[$v_xml_tag_in_db];
				if ($v_data_format=="isdate"){
					$v_value_input = "'"._ddmmyyyy_to_yyyymmdd($v_value_input)."'";
				}elseif($v_data_format=="isnumeric"){
					$v_value_input = $v_value_input;
				}else{
					$v_value_input = "'".$v_value_input."'";
				}			
			}
			
			$v_sql_replace_temp = str_replace("#".$v_xml_tag_in_db."#",$v_value_input,$v_sql_replace_temp);
		}
		$v_table_struct_rec = $v_table_struct_rax->readRecord();
	}
	return  $v_sql_replace_temp;
}
function is_data_valid($v_recordset,$v_xml_data,$v_xml_compare){
	$v_received_record_xml_data = $v_recordset['C_RECEIVED_RECORD_XML_DATA'];
	$v_xml_data_temp = $v_xml_data;
	$v_table_struct_rax = new RAX(); 
	$v_table_struct_rec = new RAX(); 
	$v_table_struct_rax->open($v_xml_data);
	$v_table_struct_rax->record_delim = 'row';
	$v_table_struct_rax->parse();
	$v_table_struct_rec = $v_table_struct_rax->readRecord(); 
	while ($v_table_struct_rec) { 
		$v_table_struct_row = $v_table_struct_rec->getRow();
		$v_tag_list = $v_table_struct_row["tag_list"];
		$arr_tag = explode(",", $v_tag_list);
		for($i=0;$i < sizeof($arr_tag); $i++){
			$v_formfield_rax = new RAX(); 
			$v_formfield_rec = new RAX(); 
			$v_formfield_rax->open($v_xml_data_temp);
			$v_formfield_rax->record_delim = $arr_tag[$i];
			$v_formfield_rax->parse();
			$v_formfield_rec = $v_formfield_rax->readRecord(); 			
			$v_formfield_row = $v_formfield_rec->getRow(); 
			$v_data_format = $v_formfield_row["data_format"];
			$v_xml_data = $v_formfield_row["xml_data"];
			$v_xml_tag_in_db = $v_formfield_row["xml_tag_in_db"];
			$v_colume_name = $v_formfield_row["colume_name"];
			$v_compare_operator = _restore_XML_bad_char($v_formfield_row["compare_operator"]);
			if ($v_xml_compare!=""){
				$v_column_rax = new RAX(); 
				$v_column_rec = new RAX();
				$v_column_rax->open($v_xml_compare);
				$v_column_rax->record_delim = 'data_list';
				$v_column_rax->parse();
				$v_column_rec = $v_column_rax->readRecord(); 
				$v_column_row = $v_column_rec->getRow();
				$v_value_input = $v_column_row[$v_xml_tag_in_db];			
			}
			//Kiem tra neu nhu gia tri nhap vao ma rong hoac null thi khong can phai so sanh
			if (!is_null($v_value_input) && (trim($v_value_input)!="")){
				if ($v_xml_data == "true"){
					if ($v_received_record_xml_data!=""){
							$v_column_rax = new RAX(); 
							$v_column_rec = new RAX();
							$v_column_rax->open($v_received_record_xml_data);
							$v_column_rax->record_delim = 'data_list';
							$v_column_rax->parse();
							$v_column_rec = $v_column_rax->readRecord(); 
							$v_column_row = $v_column_rec->getRow();
							$v_value1 = $v_column_row[$v_xml_tag_in_db];			
					}
					$v_value2 = $v_value_input;			
				}else{
					if ($v_data_format=="isdate"){
						$v_value1 = strtotime(_yyyymmdd_to_ddmmyyyy($v_recordset[$v_colume_name]));				
						$v_value2 = strtotime($v_value_input);
					}else{
						$v_value1 = $v_recordset[$v_colume_name];
						$v_value2 = $v_value_input;
					}
				}
				if (!data_type_compare($v_compare_operator,$v_value1,$v_value2)){
					return false;
				}
			} 
		}
		$v_table_struct_rec = $v_table_struct_rax->readRecord();
	}
	return  true;
}
function data_type_compare($p_operator,$p_value1,$p_value2){
	switch($p_operator) {	
		case ">";
			$v_ok = ($p_value1>$p_value2);
			break;
		case "<";
			$v_ok = ($p_value1<$p_value2);
			break;
		case ">=";
			$v_ok = ($p_value1>=$p_value2);
			break;
		case "<=";
			$v_ok = ($p_value1<=$p_value2);
			break;
		case "=";
			$v_ok = ($p_value1==$p_value2);
		case "==";
			$v_ok = ($p_value1==$p_value2);
			break;
		case "in";
			$v_ok = stristr($p_value1, $p_value2); 
			break;
		default:
			$v_ok = false;
	}
	return 	$v_ok;
}
//CN : Lay ta ca cac NSD trong enduser va sap xep theo NSD trong tung phong
function get_all_enduser($arr_allenduser){
	$i=0;
	foreach($_SESSION['arr_all_unit'] as $unit_item){
		if(!is_null($unit_item['parent_id']) && ($unit_item['parent_id']<>"")){
			foreach($arr_allenduser as $staff_item){
				if($unit_item['id'] == $staff_item['unit_id']){
					$arr_return[$i][0] = $staff_item['id'];
					$arr_return[$i][1] = $staff_item['name'];
					$arr_return[$i][2] = $staff_item['unit_id'];
					$arr_return[$i][3] = $unit_item['name'];
					$arr_return[$i][4] = $staff_item[$i][3];
					$i++;
				}	
			}	
		}	
	}	
	return $arr_return;
}
function get_arr_all_staff($arr_all_staff){
	$i=0;
	foreach($arr_all_staff as $staff_item) {
		$arr_return[$i][0]=$staff_item['id'];
		$arr_return[$i][1]=$staff_item['unit_id'];
		$arr_return[$i][2]=$staff_item['code'];
		$arr_return[$i][3]=$staff_item['name'];
		$arr_return[$i][4]=1;
		$arr_return[$i][5]=1;
		$i++;
	}
	return $arr_return;
}
//Doi mang tu session ra mang thuong
function get_arr_all_unit($arr_all_unit){
	$i=0;
	foreach($arr_all_unit as $arrInsideArray){
		$arr_child_unit[$i][0] = $arrInsideArray['id'];
		$arr_child_unit[$i][1] = $arrInsideArray['parent_id'];
		$arr_child_unit[$i][2] = $arrInsideArray['code'];
		$arr_child_unit[$i][3] = $arrInsideArray['name'];
		$arr_child_unit[$i][4] = 0;
		$arr_child_unit[$i][5] = 0;
		$i++;
	}
	return $arr_child_unit;
}
function get_arr_child_staff($arr_unit){
	$i = 0;
	$v_count = sizeof($arr_unit);
	for($j=0; $j < $v_count; $j++){
		foreach($_SESSION['arr_all_staff'] as $v_staff){
			if($v_staff['unit_id'] == $arr_unit[$j]['0']){
				$arr_child_staff[$i][0] = $v_staff['id'];
				$arr_child_staff[$i][1] = $v_staff['unit_id'];
				$arr_child_staff[$i][2] = $v_staff['code'];
				$arr_child_staff[$i][3] = $v_staff['name'];
				$arr_child_staff[$i][4] = 1;
				$arr_child_staff[$i][5] = 1;
				$i++;
			}
		}
	}
	return $arr_child_staff;
}
function get_arr_child_unit($unit_id){
	//Gan ca unit cap 1 vao mang
	$arr_child_unit[0][0] = $unit_id;
	$arr_child_unit[0][1] = NULL;
	$arr_child_unit[0][2] = _get_item_attr_by_id($_SESSION['arr_all_unit'],$unit_id,'code');
	$arr_child_unit[0][3] = _get_item_attr_by_id($_SESSION['arr_all_unit'],$unit_id,'name');
	$arr_child_unit[0][4] = 0;
	$arr_child_unit[0][5] = 0;
	$i = 1;
	foreach($_SESSION['arr_all_unit'] as $v_unit){
		if ($v_unit['parent_id'] == $unit_id){
			$arr_child_unit[$i][0] = $v_unit['id'];
			$arr_child_unit[$i][1] = $v_unit['parent_id'];
			$arr_child_unit[$i][2] = $v_unit['code'];
			$arr_child_unit[$i][3] = $v_unit['name'];
			$arr_child_unit[$i][4] = 0;
			$arr_child_unit[$i][5] = 0;
			$i++;
		}
	}
	return $arr_child_unit;	
}
//Ham tra lai ten cua trang thai ho so
function get_text_record_status($v_value){
	global $arr_record_status;
	for ($i=0;$i<sizeof($arr_record_status);$i++){
		if ($arr_record_status[$i]['0'] == $v_value){
			return $arr_record_status[$i]['1'];
		}
	}
	return "";
}
function get_text_list_status($v_value){
	global $arr_list_status;
/*	if($v_value=='true'){
		$v_value=1;
	}
	else{
		$v_value=0;
	}
*/	
	for ($i=0;$i<sizeof($arr_list_status);$i++){
		if ($arr_list_status[$i]['0'] == $v_value){
			return $arr_list_status[$i]['1'];
		}
	}
	return "";
}
//Ham tra lai ten cua nguoi su dung dua vao id
function get_text_staff_by_id($v_value){
	foreach($_SESSION['arr_all_staff'] as $v_staff){
		if($v_staff['id'] == $v_value){
			return $v_staff['name'];
		}
	}
	return "";
}

//Lay ten chu dau tu cua cac TTHC thuoc phong HTKT
function get_investor_by_recordtype($v_xml_data,$v_recordtype_code){
	switch($v_recordtype_code){
		case "009";
			return _XML_get_xml_tag_value($v_xml_data,"registor_name","registor_name");
			break;
		case "011";
			return _XML_get_xml_tag_value($v_xml_data,"registor_name","registor_name");
			break;
		case "012";
			return _XML_get_xml_tag_value($v_xml_data,"registor_name","registor_name");
			break;
		case "014";
			return _XML_get_xml_tag_value($v_xml_data,"registor_name","registor_name");
			break;
		case "013";
			return _XML_get_xml_tag_value($v_xml_data,"registor_name","registor_name");
			break;
	}
}	

//Chuc nang: Hien thi thong tin chung cua ho so va cac lien ket thuc hien cac tac nghiep vo ho so
function onegate_show_record_general_info($p_arr_record,$p_arr_recordtype,$p_show_link,$p_petition_link,$p_rate_of_progress_link,$p_document_link,$p_idea_link,$p_sign_link,$p_approve_link,$p_licensing_link,$p_update_record_status){
	global $arr_record_status,$v_record_id,$_ISA_CURRENT_MODUL_CODE,$_ISA_WEB_SITE_PATH;
	$p_post_license_link = $_ISA_WEB_SITE_PATH."record/archives/index.php?fuseaction=DISPLAY_SINGLE_POST_LICENSE&hdn_record_id=".$v_record_id; //dung cho link xem noi dung xu ly sau giai quyet
	//var_dump($p_arr_recordtype);
	$v_staff_id =      $_SESSION['staff_id'];
	$v_arr_all_staff = $_SESSION['arr_all_staff'];
	$v_arr_all_unit =  $_SESSION['arr_all_unit'];
	$v_staff_name = _get_item_attr_by_id($v_arr_all_staff,$v_staff_id,'name');
	$v_record_id = $p_arr_record['PK_RECORD'];
	$v_recordtype = $p_arr_record['FK_RECORDTYPE'];
	$v_receiver_id = $p_arr_record['FK_RECEIVER'];
	$v_handler_id = $p_arr_record['FK_HANDLER'];
	$v_record_code = $p_arr_record['C_CODE'];
	$v_current_status = $p_arr_record['C_CURRENT_STATUS'];
	$v_detail_status = $p_arr_record['C_DETAIL_STATUS'];
	$v_received_date = _yyyymmdd_to_ddmmyyyy($p_arr_record['C_RECEIVED_DATE']);
	$v_appointed_date = _yyyymmdd_to_ddmmyyyy($p_arr_record['C_APPOINTED_DATE']);
	$v_handle_leader_id = $p_arr_record['FK_HANDLE_LEADER'];
	$v_approve_leader_id = $p_arr_record['FK_APPROVE_LEADER'];
	$v_recordtype_name = $p_arr_record['C_RECORDTYPE_NAME'];
	$v_license_xml_data = $p_arr_record['C_LICENSE_XML_DATA'];
	$v_reason = $p_arr_record['C_REASON'];
	
	//--- Bien lay ten chu dau tu cac TTHC phong HTKT---hienmt---- 
	if($v_recordtype=='009' || $v_recordtype=='011' || $v_recordtype=='012' || $v_recordtype=='013' || $v_recordtype=='014')	$v_chu_dau_tu = get_investor_by_recordtype($p_arr_record['C_RECEIVED_RECORD_XML_DATA'],$v_recordtype);
	//----------end------------
	/*$v_current_status_name = "";
	for ($i=0;$i<sizeof($arr_record_status);$i++){
		if ($arr_record_status[$i]['0']==$v_current_status){
			$v_current_status_name = $arr_record_status[$i]['1'];
			break;
		}
	}*/
	$v_current_status_name = $p_arr_record['C_CURRENT_STATUS_VUNH'];
	for ($i=0;$i<sizeof($p_arr_recordtype);$i++){
		if ($p_arr_recordtype[$i]['PK_RECORDTYPE']==$v_recordtype){
			$v_handler_unit_id = $p_arr_recordtype[$i]['FK_HANDLE_AGENCY'];
			//$v_handler_id = $p_arr_recordtype[$i]['FK_HANDLER'];
			break;
		}
	}
	$v_handler_unit_name = _get_item_attr_by_id($v_arr_all_unit,$v_handler_unit_id,'name');
	$v_handler_name = _get_item_attr_by_id($v_arr_all_staff,$v_handler_id,'name');

	// Lay label de hien thi link "Noi dung van ban tra ket qua"
	$arr_result_label = onegate_get_label_of_result_type($p_arr_recordtype,$v_recordtype);

	$strHTML = "";
	$strHTML = $strHTML . "<table width='99%' cellpadding='0' cellspacing='0'>";
	$strHTML = $strHTML . "<col width='25%'><col width='25%'><col width='25%'><col width='25%'>";
	$strHTML = $strHTML . "<tr>";
	$strHTML = $strHTML . "<td class='normal_label'>Lo&#7841;i h&#7891; s&#417;</td>";
	$strHTML = $strHTML . "<td class='normal_label' colspan='3'>$v_recordtype_name</td>";
	$strHTML = $strHTML . "</tr><tr>";
	$strHTML = $strHTML . "<td class='normal_label'>M&#227; h&#7891; s&#417;</td>";
	$strHTML = $strHTML . "<td class='normal_label'>$v_record_code</td>";
	//---Them chu dau tu vao phan tien do cac thu tuc thuoc phong Ha Tang Kinh Te--hienmt-------
	if($v_recordtype=='009' || $v_recordtype=='011' || $v_recordtype=='012' || $v_recordtype=='013' || $v_recordtype=='014'){
		$strHTML = $strHTML . "<td class='normal_label'>Ng&#432;&#7901;i &#273;&#7841;i di&#7879;n</td>";
		$strHTML = $strHTML . "<td class='normal_label'>$v_chu_dau_tu</td>";
	}
	//-------- End -------------------
	$strHTML = $strHTML . "</tr><tr>";
	$strHTML = $strHTML . "<td class='normal_label'>Ng&#224;y ti&#7871;p nh&#7853;n</td>";
	$strHTML = $strHTML . "<td class='normal_label'>$v_received_date</td>";
	$strHTML = $strHTML . "<td class='normal_label'>Ng&#224;y h&#7865;n tr&#7843; k&#7871;t qu&#7843;</td>";
	$strHTML = $strHTML . "<td class='normal_label'>$v_appointed_date</td>";
	$strHTML = $strHTML . "</tr><tr>";
	$strHTML = $strHTML . "<td class='normal_label'>Ph&#242;ng th&#7909; l&#253;</td>";
	$strHTML = $strHTML . "<td class='normal_label'>$v_handler_unit_name</td>";
	$strHTML = $strHTML . "<td class='normal_label'>C&#225;n b&#7897; th&#7909; l&#253;</td>";
	$strHTML = $strHTML . "<td class='normal_label'>$v_handler_name</td>";
	$strHTML = $strHTML . "</tr><tr>";
	$strHTML = $strHTML . "<td class='normal_label'>Tr&#7841;ng th&#225;i h&#7891; s&#417;</td>";
	$strHTML = $strHTML . "<td class='normal_label'>$v_current_status_name</td>";
	$strHTML = $strHTML . "</tr>";
	if (!is_null($v_reason) && trim($v_reason)!=""){
		$strHTML = $strHTML . "<tr><td class='normal_label'>L&#253; do t&#7915; ch&#7889;i</td>";
		$strHTML = $strHTML . "<td class='normal_label' colspan='4'><font color='#FF3333'>$v_reason</font></td>";
		$strHTML = $strHTML . "</tr>";
	}
	if ($p_show_link==true){
		$strHTML = $strHTML . "<tr>";
		$strHTML = $strHTML . "<td class='large_link' colspan='5' align='left'>";
		$strHTML = $strHTML . "&nbsp;&nbsp;<a href='$p_petition_link'>&#272;&#417;n</a>";
		$strHTML = $strHTML . "&nbsp;&nbsp;|&nbsp;&nbsp;<a href='$p_rate_of_progress_link'>Ti&#7871;n &#273;&#7897;</a>";
		$strHTML = $strHTML . "&nbsp;&nbsp;|&nbsp;&nbsp;<a href='$p_document_link'>T&#224;i li&#7879;u</a>";
		$strHTML = $strHTML . "&nbsp;&nbsp;|&nbsp;&nbsp;<a href='$p_idea_link'>&#221; ki&#7871;n</a>";
		if (onegate_is_handler_for_recordtype($v_staff_id, $v_recordtype, $_SESSION["recordtype"]) && $_ISA_CURRENT_MODUL_CODE=="HANDLE" && $v_current_status=="DANG_THU_LY" && $v_detail_status!=20){
			$strHTML = $strHTML . "&nbsp;&nbsp;|&nbsp;&nbsp;<a href='$p_update_record_status'>K&#7871;t qu&#7843; th&#7909; l&#253;</a>";
		}	
		if ((onegate_is_main_handler_for_record($v_staff_id,$v_record_id) || ($v_license_xml_data!="" && !is_null($v_license_xml_data))) && $v_detail_status!=20 ){
			$strHTML = $strHTML . "&nbsp;&nbsp;|&nbsp;&nbsp;<a href='$p_licensing_link'>" . $arr_result_label[3] . "</a>";
		}
		  
		if ($_ISA_CURRENT_MODUL_CODE=="ARCHIVES"){
			$strHTML = $strHTML . "&nbsp;&nbsp;|&nbsp;&nbsp;<a href='$p_post_license_link'>X&#7917; l&#253; sau gi&#7843;i quy&#7871;t</a>";
		}
		
		$strHTML = $strHTML . "</td>";
		$strHTML = $strHTML . "</tr>";
	}
	$strHTML = $strHTML ."</table>";
	return $strHTML;
}
// Update by Dinh, Nguyen - 03072007
//Ham tu sinh ma theo tung loai ho so va theo tung nam
function onegate_generate_record_code($v_recordtype){
	global $_ISA_OWNER_CODE;
	global $ado_conn;
	$v_inc_code_length = 5;//Do dai cua ma theo tung loai, tung nam cua ho so
	$v_fix_code = date("y") .".". onegate_get_code_record($v_recordtype);
	$v_str_count = strlen($v_fix_code);
	
	$v_str_sql = " Select count(*) MAX_CODE ";
	$v_str_sql = $v_str_sql." From T_ONEGATE_RECORD Where 1=1";
	if(($v_recordtype <> '001') && ($v_recordtype <> '002')){
		$v_str_sql = $v_str_sql." And SUBSTRING(C_CODE,1 + $v_inc_code_length,$v_str_count+1) = '.$v_fix_code' And LEN(C_CODE)=($v_inc_code_length+$v_str_count+1)";	
	}else{
		$v_str_sql = $v_str_sql." And FK_RECORDTYPE = " . "'" . $v_recordtype ."'";	
	}	
	//echo $v_str_sql, "<br>";
	//Lay so ho so hien tai
	$v_sql = "Select C_BEGIN_RECORD_CODE From T_ONEGATE_RECORDTYPE where PK_RECORDTYPE = " . "'" . $v_recordtype ."'";	
	//echo $v_sql, "<br>";
	if(_is_sqlserver()){
		$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
		$arr_all_data = $ado_conn->GetRow($v_str_sql); 
		$arr_single_begin_record_num = $ado_conn->GetRow($v_sql); 
	}
	$v_bebin_record_num = 0;
	if(sizeof($arr_single_begin_record_num) > 0){
		$v_bebin_record_num = $arr_single_begin_record_num['C_BEGIN_RECORD_CODE'];
	}	
	$v_next_code = $arr_all_data['MAX_CODE'] + $v_bebin_record_num;
	//echo $v_next_code, "<br>";
	if (is_null($v_next_code) || $v_next_code==""){
		$v_next_code = 1 + $v_bebin_record_num;
		$v_next_code = str_repeat("0",$v_inc_code_length-strlen($v_next_code)).$v_next_code;
	}else{
		$v_next_code = intval($v_next_code)+1;
		$v_next_code = str_repeat("0",$v_inc_code_length-strlen($v_next_code)).$v_next_code;
	}
	$v_str_ret = $v_next_code . "." . $v_fix_code;
	while(!dinh_check_c_code($v_str_ret, $v_recordtype)){	# neu la false thi lam tiep
		$v_next_code++;
		$v_next_code = str_repeat("0",$v_inc_code_length-strlen($v_next_code)).$v_next_code;
		$v_str_ret = $v_next_code . "." . $v_fix_code;
	}
	//echo $v_str_ret; exit;
	return $v_str_ret;
}
//----- Created by: Dinh, Nguyen
# Date: 03/07/2007
# Content: Fix van de phat sinh ma bi trung, sau khi delete
# Target: increase until ok
function dinh_check_c_code($v_str_ret, $v_recordtype){

	global $_ISA_OWNER_CODE;
	global $ado_conn;

	$dinh_ret = true;

	$sql = "select C_CODE from T_ONEGATE_RECORD where FK_RECORDTYPE = ";
	$sql .= "'".$v_recordtype."'";
	$sql .= "AND C_CODE = ";
	$sql .= "'".$v_str_ret."'";
	if(_is_sqlserver()){
		$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
		$arr_data = $ado_conn->GetRow($sql);
	}
	if(sizeof($arr_data) > 0)
		$dinh_ret = false;
return $dinh_ret;
}

//---End  Update by Dinh, Nguyen - 03072007

////////////////////
function onegate_get_code_record($v_recordtype){
	switch($v_recordtype){
		/*case "001";
		return "HKD";
		break;
		case "002";
		return "HTX";
		break;
		//Cap giay chung nhan quyen su dung dat
		case "004";
		return "VPDK";
		break;
		//Danh ky the chap
		case "004a";
		return "DKTC";
		break;
		//Dang ky bao lanh
		case "004b";
		return "DKBL";
		break;
		//Thay doi the chap, bao lanh
		case "004c";
		return "TDTCBL";
		break;
		//Xao dang ky the chap bao lanh
		case "004d";
		return "XTCBL";
		break;
		case "009";
		return "SGHT";
		break;
		case "009a";
		return "CLKS";
		break;
		case "009b";
		return "TDKS";
		break;
		case "010";
		return "THSGHT";
		break;
		case "011";
		return "CTHUC";
		break;
		case "012";
		return "THCT";
		break;
		case "020";
		return "HK";
		break;
		case "021a";
		return "XNDC";
		case "021b";
		return "XNCC";		
		break;		
		case "021c";
		return "MGTD";		
		break;		
		case "021d";
		return "MGHN";		
		break;		
		case "021e";
		return "TVNHHC";		
		break;				
		case "021f";
		return "CLTB";		
		break;				
		case "022";
		return "UDNCC";
		break;		
		case "022a";
		return "UDGD";
		break;		
		case "023";
		return "TVTNLS";
		break;		
		case "007e";
		return "CPXD";
		break;
		case "025";
		return "DKSDLD";
		break;*/
	default:
		return $v_recordtype;
	}
}
function onegate_generate_record_code_net($v_recordtype){
	global $_ISA_OWNER_CODE;
	global $ado_conn;
	$v_inc_code_length = 5;//Do dai cua ma theo tung loai, tung nam cua ho so
	$v_fix_code = date("y") . ".NET" . ".". onegate_get_code_record($v_recordtype);
	$v_str_count = strlen($v_fix_code);
	//$v_str_sql = " Select Max(SUBSTRING(C_CODE, 1, $v_inc_code_length)) MAX_CODE ";
	$v_str_sql = " Select count(*) MAX_CODE ";
	$v_str_sql = $v_str_sql." From T_ONEGATE_CUSTOMER_RECORD";
	$v_str_sql = $v_str_sql." Where SUBSTRING(C_CODE,1 + $v_inc_code_length,$v_str_count+1) = '.$v_fix_code' And LEN(C_CODE)=($v_inc_code_length+$v_str_count+1)";	
	//echo $v_str_sql.'<br><br><br><br>';
	if(_is_sqlserver()){
		$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
		$arr_all_data = $ado_conn->GetRow($v_str_sql); 
	}
	$v_next_code = $arr_all_data['MAX_CODE'];
	if (is_null($v_next_code) || $v_next_code==""){
		$v_next_code = 1;
		$v_next_code = str_repeat("0",$v_inc_code_length-strlen($v_next_code)).$v_next_code;
	}else{
		$v_next_code = intval($v_next_code)+1;
		$v_next_code = str_repeat("0",$v_inc_code_length-strlen($v_next_code)).$v_next_code;
	}
	$v_str_ret = $v_next_code . "." . $v_fix_code;
	return $v_str_ret;	
}
function doCleanCode($code) {
	$code =preg_replace("/<p([^>])*>(&nbsp;)*\s*<\/p>/i","",$code);
	return $code;
}
function onegate_str_replace_char($v_html){
	$v_html = str_replace('&amp;','&',$v_html);
	$v_html = str_replace('&quot;','"',$v_html);
	$v_html = str_replace('&#39;',"'",$v_html);
	$v_html = str_replace('&lt;','<',$v_html);
	$v_html = str_replace('&gt;','>',$v_html);
	return $v_html;
}
function onegate_data_format($p_value){
	$v_ret_value = strval($p_value);
	if ($v_ret_value=="" || is_null($v_ret_value)){
		return "";
	}
	$arr_value=explode(".",$v_ret_value);
	if (isset($arr_value[1]) && $arr_value[1]*1==0){
		$v_ret_value = $arr_value[0];
	}
	if (strpos($v_ret_value,".")===false){
		$v_ret_value = number_format($v_ret_value, 0, '.', ',');
	}else{
		$v_ret_value = number_format($v_ret_value, 2, '.', ',');
	}	
	if ($v_ret_value == "0.00") $v_ret_value = "0";
	return $v_ret_value;
}
//Ham so sanh gia tri cua so  DUNG CHO SAP XEP DU LIEU
function sort_number_asc($a, $b){
	global $v_group_by,$v_xml_data_compare;
	if ($v_xml_data_compare == "true"){
		$v_xml_string_a = $a['C_RECEIVED_RECORD_XML_DATA'];
		$v_xml_string_b = $b['C_RECEIVED_RECORD_XML_DATA'];
		//Lay gia tri tu mang a
		$column_rax = new RAX(); 
		$column_rec = new RAX();
		$column_rax->open($v_xml_string_a);
		$column_rax->record_delim = 'data_list';
		$column_rax->parse();
		$column_rec = $column_rax->readRecord(); 
		$column_row = $column_rec->getRow();
		$v_value_a = _restore_XML_bad_char($column_row[$v_group_by]);
		//Lay gia tri tu mang b
		$column_rax = new RAX(); 
		$column_rec = new RAX();
		$column_rax->open($v_xml_string_b);
		$column_rax->record_delim = 'data_list';
		$column_rax->parse();
		$column_rec = $column_rax->readRecord(); 
		$column_row = $column_rec->getRow();
		$v_value_b = _restore_XML_bad_char($column_row[$v_group_by]);
		if ($v_value_a == $v_value_b) {
			return 0;
		}
		return ($v_value_a < $v_value_b) ? -1 : 1;
	}else{
		$v_value_a = $a[$v_group_by];
		$v_value_b = $b[$v_group_by]; 
		if ($v_value_a == $v_value_b) {
			return 0;
		}
		return ($v_value_a < $v_value_b) ? -1 : 1;
	}
}
//Ham so sanh gia tri cua xau  DUNG CHO SAP XEP DU LIEU
function sort_string_asc($a, $b){
	global $v_group_by,$v_xml_data_compare;
	if ($v_xml_data_compare == "true"){
		$v_xml_string_a = $a['C_RECEIVED_RECORD_XML_DATA'];
		$v_xml_string_b = $b['C_RECEIVED_RECORD_XML_DATA'];
		//Lay gia tri tu mang a
		$column_rax = new RAX(); 
		$column_rec = new RAX();
		$column_rax->open($v_xml_string_a);
		$column_rax->record_delim = 'data_list';
		$column_rax->parse();
		$column_rec = $column_rax->readRecord(); 
		$column_row = $column_rec->getRow();
		$v_value_a = _restore_XML_bad_char($column_row[$v_group_by]);
		//Lay gia tri tu mang b
		$column_rax = new RAX(); 
		$column_rec = new RAX();
		$column_rax->open($v_xml_string_b);
		$column_rax->record_delim = 'data_list';
		$column_rax->parse();
		$column_rec = $column_rax->readRecord(); 
		$column_row = $column_rec->getRow();
		$v_value_b = _restore_XML_bad_char($column_row[$v_group_by]);
		return strcmp($v_value_a, $v_value_b);
	}else{
		return strcmp($a[$v_group_by],$b[$v_group_by]); 		
	}
}
//Cac ham kiem tra vai tro NSD
//Ham kiem tra vai tro cua 1 NSD co phai la can bo tiep nhan doi voi 1 loai ho so dang ky qua mang hay khong
function onegate_is_receiver_for_recordtype_net($p_staff_id, $p_recordtype_id, $p_arr_all_recordtype){
	if(sizeof($p_arr_all_recordtype) <= 0){
		return false;
	}else{
		$k = 0;
		if ($p_recordtype_id == 0){
			for($i=0;$i<sizeof($p_arr_all_recordtype);$i++){
				if ($p_arr_all_recordtype[$i]['C_IS_REGISTER_ON_NET']==1){
					$arr_all_receiver = split(",",$p_arr_all_recordtype[$i]['FK_RECEIVER_LIST']);
					for($v_index = 0;$v_index<sizeof($arr_all_receiver);$v_index++){
						if ($arr_all_receiver[$v_index] == $p_staff_id){
							$k = $k + 1;
						}
					}	
				}
			}
			if ($k <= 0 ){return false;	}else{ return true;	}
		}else{
			for($i=0; $i< sizeof($p_arr_all_recordtype); $i++){
				if ($p_arr_all_recordtype[$i]['C_IS_REGISTER_ON_NET']==1 && $p_arr_all_recordtype[$i]['PK_RECORDTYPE'] == $p_recordtype_id ){
					 $j = $i;
					$arr_all_receiver = split(",",$p_arr_all_recordtype[$j]['FK_RECEIVER_LIST']);
					for($v_index = 0;$v_index<sizeof($arr_all_receiver);$v_index++){
						if ($arr_all_receiver[$v_index] == $p_staff_id){
							$k = $k + 1;
						}
					}	
				}
			}
			if ($k <= 0 ){return false;	}else{ return true;	}	
		}
	}
}
//Ham kiem tra vai tro cua 1 NSD co phai la can bo tiep nhan doi voi 1 loai ho so hay khong
function onegate_is_receiver_for_recordtype($p_staff_id, $p_recordtype_id, $p_arr_all_recordtype){
	if(sizeof($p_arr_all_recordtype) <= 0){
		return false;
	}else{
		$k = 0;
		if ($p_recordtype_id == 0){
			for($i=0;$i<sizeof($p_arr_all_recordtype);$i++){
				$arr_all_receiver = split(",",$p_arr_all_recordtype[$i]['FK_RECEIVER_LIST']);
				for($v_index = 0;$v_index<sizeof($arr_all_receiver);$v_index++){
					if ($arr_all_receiver[$v_index] == $p_staff_id){
						$k = $k + 1;
					}
				}	
			}
			if ($k <= 0 ){return false;	}else{ return true;	}
		}else{
			for($i=0; $i< sizeof($p_arr_all_recordtype); $i++){
				if ($p_arr_all_recordtype[$i]['PK_RECORDTYPE'] == $p_recordtype_id ){
					 $j = $i;
					$arr_all_receiver = split(",",$p_arr_all_recordtype[$j]['FK_RECEIVER_LIST']);
					for($v_index = 0;$v_index<sizeof($arr_all_receiver);$v_index++){
						if ($arr_all_receiver[$v_index] == $p_staff_id){
							$k = $k + 1;
						}
					}	
				}
			}			
			if ($k <= 0 ){return false;	}else{ return true;	}	
		}
	}	
}
//Ham kiem tra vai tro cua 1 NSD co phai la can bo thu ly doi voi 1 loai ho so hay khong
function onegate_is_handler_for_recordtype($p_staff_id, $p_recordtype_id, $p_arr_all_recordtype){
	if(sizeof($p_arr_all_recordtype) <= 0){
		return false;
	}else{
		$k = 0;
		if ($p_recordtype_id <= 0){
			for($i=0; $i< sizeof($p_arr_all_recordtype); $i++){
				if (_list_have_element($p_arr_all_recordtype[$i]['FK_HANDLER_LIST'],$p_staff_id,',')==true){
					$k = $k + 1;
				}
			}
			if ($k <= 0 ){return false;	}else{ return true;	}
		}else{
			for($i=0; $i< sizeof($p_arr_all_recordtype); $i++){
				if ($p_arr_all_recordtype[$i]['PK_RECORDTYPE'] == $p_recordtype_id) {
					$j = $i;
					if (_list_have_element($p_arr_all_recordtype[$j]['FK_HANDLER_LIST'],$p_staff_id,',')==true){
						$k = $k + 1;
					}
				}
			}
			if ($k <= 0 ){return false;	}else{ return true;	}	
		}
	}
}
//Ham kiem tra vai tro cua 1 NSD co phai la lanh dao phu trach duyet hs lan thu nhat khong?
function onegate_is_handler_leader_first_time_for_recordtype($p_staff_id, $p_recordtype_id, $p_arr_all_recordtype){
	if(sizeof($p_arr_all_recordtype) <= 0){
		return false;
	}else{
		$k = 0;
		if ($p_recordtype_id <= 0){
			for($i=0; $i< sizeof($p_arr_all_recordtype); $i++){
				if ($p_arr_all_recordtype[$i]['FK_HANDLE_LEADER_FIRST_TIME'] == $p_staff_id){
					$k = $k + 1;
				}
			}
			if ($k <= 0 ){return false;	}else{ return true;	}	
		}else{
			for($i=0; $i< sizeof($p_arr_all_recordtype); $i++){
				if ($p_arr_all_recordtype[$i]['PK_RECORDTYPE'] == $p_recordtype_id ){
					$j = $i;
					if ($p_arr_all_recordtype[$j]['FK_HANDLE_LEADER_FIRST_TIME'] == $p_staff_id ){
						$k = $k + 1;
					}
				}
			}
			if ($k <= 0 ){return false;	}else{ return true;	}	
		}
	}
}
//Ham kiem tra vai tro cua 1 NSD co phai la lanh dao phu trach thu ly doi voi loai ho so hay khong
function onegate_is_handler_leader_for_recordtype($p_staff_id, $p_recordtype_id, $p_arr_all_recordtype){
	if(sizeof($p_arr_all_recordtype) <= 0){
		return false;
	}else{
		$k = 0;
		if ($p_recordtype_id <= 0){
			for($i=0; $i< sizeof($p_arr_all_recordtype); $i++){
				if ($p_arr_all_recordtype[$i]['FK_HANDLE_LEADER'] == $p_staff_id){
					$k = $k + 1;
				}
			}
			if ($k <= 0 ){return false;	}else{ return true;	}	
		}else{
			for($i=0; $i< sizeof($p_arr_all_recordtype); $i++){
				if ($p_arr_all_recordtype[$i]['PK_RECORDTYPE'] == $p_recordtype_id ){
					$j = $i;
					if ($p_arr_all_recordtype[$j]['FK_HANDLE_LEADER'] == $p_staff_id ){
						$k = $k + 1;
					}
				}
			}
			if ($k <= 0 ){return false;	}else{ return true;	}	
		}
	}
}
//Ham kiem tra vai tro cua 1 NSD co phai la lanh dao phu trach ky duyet doi voi loai ho so hay khong
function onegate_is_approve_leader_for_recordtype($p_staff_id, $p_recordtype_id, $p_arr_all_recordtype){
	if(sizeof($p_arr_all_recordtype) <= 0){
		return false;
	}else{
		$k = 0;
		if ($p_recordtype_id <= 0){
			for($i=0; $i< sizeof($p_arr_all_recordtype); $i++){
				if (_list_have_element($p_arr_all_recordtype[$i]['FK_APPROVE_LEADER_LIST'],$p_staff_id,',')){
				//if ($p_arr_all_recordtype[$i]['FK_APPROVE_LEADER_LIST'] == $p_staff_id){
					$k = $k + 1;
				}
			}
			if ($k <= 0 ){return false;	}else{ return true;	}
		}else{
			for($i=0; $i< sizeof($p_arr_all_recordtype); $i++){
				if ($p_arr_all_recordtype[$i]['PK_RECORDTYPE'] == $p_recordtype_id ){
					$j = $i;
					if (_list_have_element($p_arr_all_recordtype[$j]['FK_APPROVE_LEADER_LIST'],$p_staff_id,',')){
					//if ($p_arr_all_recordtype[$j]['FK_APPROVE_LEADER'] == $p_staff_id){
						$k = $k + 1;
					}
				}
			}
			if ($k <= 0 ){return false;	}else{ return true;	}
		}
	}
}
//Ham kiem tra vai tro cua 1 NSD co duoc cap nhat tien do cong viec khong?
function onegate_is_permission_update_rate_of_progress_for_recordtype($p_staff_id, $p_recordtype_id, $p_arr_all_recordtype){
	if(sizeof($p_arr_all_recordtype) <= 0){
		return false;
	}else{
		$k = 0;
		if ($p_recordtype_id <= 0){
			for($i=0; $i< sizeof($p_arr_all_recordtype); $i++){
				if (_list_have_element($p_arr_all_recordtype[$i]['C_STAFF_ID_LIST'],$p_staff_id,',')==true){
					$k = $k + 1;
				}
			}
			if ($k <= 0 ){return false;	}else{ return true;	}
		}else{
			for($i=0; $i< sizeof($p_arr_all_recordtype); $i++){
				if ($p_arr_all_recordtype[$i]['PK_RECORDTYPE'] == $p_recordtype_id) {
					$j = $i;
					if (_list_have_element($p_arr_all_recordtype[$j]['C_STAFF_ID_LIST'],$p_staff_id,',')==true){
						$k = $k + 1;
					}
				}
			}
			if ($k <= 0 ){return false;	}else{ return true;	}	
		}
	}
}
//Ham kiem tra vai tro cua 1 NSD co phai la can bo thu ly chinh doi voi 1 ho so cu the hay khong
function onegate_is_main_handler_for_record($p_staff_id, $p_record_id){
	$sql = " Exec Onegate_CheckMainHandlerOnRecord ";
	$sql = $sql . $p_record_id;
	$sql = $sql . ",'".$p_staff_id."'";
	$arr_data = Query_Data_For_Colunm_name($sql);
	return $arr_data[0]['MAIN_HANDLER_ON_RECORD'];
}
//Ham kiem tra vai tro cua 1 NSD co phai la can bo phoi hop thu ly doi voi 1 ho so cu the hay khong
function onegate_is_co_handler_for_record($p_staff_id, $p_record_id){
	$sql = " Exec Onegate_CheckCoHandlerOnRecord ";
	$sql = $sql . $p_record_id;
	$sql = $sql . ",'".$p_staff_id."'";
	$sql = $sql . ",'"._CONST_DECIMAL_DELIMITOR."'";
	$arr_data = Query_Data_For_Colunm_name($sql);
	return $arr_data[0]['CO_HANDLER_ON_RECORD'];
}
//Ham kiem tra vai tro cua 1 NSD thuoc loai lanh dao phe duyet nao
function onegate_is_approve_leader_type($p_staff_id, $p_recordtype_id, $p_arr_all_recordtype){
	$v_leader_type = -1;
	$v_approve_leader = -1;
	$v_approve_leader = -1;
	if(sizeof($p_arr_all_recordtype) > 0){
		if ($p_recordtype_id !=""){
			for($i=0; $i< sizeof($p_arr_all_recordtype); $i++){
				if ($p_arr_all_recordtype[$i]['PK_RECORDTYPE'] == $p_recordtype_id ){
					$v_handle_leader = $p_arr_all_recordtype[$i]['FK_HANDLE_LEADER'];
					//$v_approve_leader = $p_arr_all_recordtype[$i]['FK_APPROVE_LEADER'];
					$index = $i;
					$v_leader_first_time = $p_arr_all_recordtype[$i]['FK_HANDLE_LEADER_FIRST_TIME'];
					break;
				}
			}			
			
			if (($p_staff_id==$v_handle_leader)&&(!_list_have_element($p_arr_all_recordtype[$index]['FK_APPROVE_LEADER_LIST'],$p_staff_id,','))){
				$v_leader_type = 0;
			}
			if (($p_staff_id==$v_handle_leader)&&(_list_have_element($p_arr_all_recordtype[$index]['FK_APPROVE_LEADER_LIST'],$p_staff_id,','))){
				$v_leader_type = 1;
			}
			if (($p_staff_id!=$v_handle_leader)&&(_list_have_element($p_arr_all_recordtype[$index]['FK_APPROVE_LEADER_LIST'],$p_staff_id,','))){
				$v_leader_type = 2;
			}
			if (($p_staff_id==$v_leader_first_time)&&($v_leader_first_time!=$v_handle_leader)){
				$v_leader_type = 3;
			}			
		}
	}
	return $v_leader_type;
}
//Ket thuc cac ham kiem tra vai tro NSD
/*Ham kiem tra loai ho so nay co chuyen co quan co tham quyen giai quyet hay khong?
  Va nguoi su dung hien thoi co phai la thu ly it nhat 1 loai ho so phai chuyen co quan co tham quyen giai quyet
*/
function onegate_is_tranfer_to_senior_for_recordtype($p_staff_id,$p_recordtype_id, $p_arr_all_recordtype){
	if(sizeof($p_arr_all_recordtype) <= 0){
		return false;
	}else{
		$k = 0;
		if ($p_recordtype_id == 0){
			for($i=0;$i<sizeof($p_arr_all_recordtype);$i++){
				if ($p_arr_all_recordtype[$i]['C_TRANSFER_TO_SENIOR']==1 && (_list_have_element($p_arr_all_recordtype[$i]['FK_HANDLER_LIST'],$p_staff_id,',')==true)){
					$k = $k + 1;
				}
			}
			if ($k <= 0 ){return false;	}else{ return true;	}
		}else{
			for($i=0; $i< sizeof($p_arr_all_recordtype); $i++){
				if ($p_arr_all_recordtype[$i]['C_TRANSFER_TO_SENIOR']==1 && $p_arr_all_recordtype[$i]['PK_RECORDTYPE'] == $p_recordtype_id ){
					$k = $k + 1;
				}
			}
			if ($k <= 0 ){return false;	}else{ return true;	}	
		}
	}
}
//chuc nang: Ham nay bien doi mot xau id cua lanh dao
function get_all_leader_convert_arr($p_recordtype_id, $p_arr_all_recordtype){
	if(sizeof($p_arr_all_recordtype) > 0){
		if ($p_recordtype_id !=""){
			for($i=0; $i< sizeof($p_arr_all_recordtype); $i++){
				if ($p_arr_all_recordtype[$i]['PK_RECORDTYPE'] == $p_recordtype_id ){
					$v_approve_leader_id = $p_arr_all_recordtype[$i]['FK_APPROVE_LEADER_LIST'];
					if(!is_null($v_approve_leader_id) && ($v_approve_leader_id<>"")){
						$arr_all_leader = split(",",$v_approve_leader_id);						
					}
					break;
				}
			}
		}
	}			
	return $arr_all_leader;
}
//------------------
//chuc nang: Ham nay kiem tra xem lanh dao duyet kq ho so lan thu nhat va thu hai co phai la mot nguoi?
function onegate_is_compare_leader($p_recordtype_id, $p_arr_all_recordtype){
	if(sizeof($p_arr_all_recordtype) > 0){
		if ($p_recordtype_id !=""){
			for($i=0; $i< sizeof($p_arr_all_recordtype); $i++){
				if ($p_arr_all_recordtype[$i]['PK_RECORDTYPE'] == $p_recordtype_id ){
					$v_leader_first_time = $p_arr_all_recordtype[$i]['FK_HANDLE_LEADER_FIRST_TIME'];
					$v_leader_second_time = $p_arr_all_recordtype[$i]['FK_HANDLE_LEADER'];
					if($v_leader_first_time<>$v_leader_second_time){
						return true;						
					}
					break;
				}
			}
		}
	}			
	return false;
}
/*Ham kiem tra loai ho so nay co bat buoc phai trinh lanh dao hay khong?
  Va nguoi su dung hien thoi co phai la thu ly it nhat 1 loai ho so bat buoc phai trinh lanh dao
*/
function onegate_is_submit_to_leader_for_recordtype($p_staff_id,$p_recordtype_id, $p_arr_all_recordtype){
	if(sizeof($p_arr_all_recordtype) <= 0){
		return false;
	}else{
		$k = 0;
		if ($p_recordtype_id == 0){
			for($i=0;$i<sizeof($p_arr_all_recordtype);$i++){
				if ($p_arr_all_recordtype[$i]['C_SUBMIT_TO_LEADER']==1 && (_list_have_element($p_arr_all_recordtype[$i]['FK_HANDLER_LIST'],$p_staff_id,',')==true)){
					$k = $k + 1;
				}
			}
			if ($k <= 0 ){return false;	}else{ return true;	}
		}else{
			for($i=0; $i< sizeof($p_arr_all_recordtype); $i++){
				if ($p_arr_all_recordtype[$i]['C_SUBMIT_TO_LEADER']==1 && $p_arr_all_recordtype[$i]['PK_RECORDTYPE'] == $p_recordtype_id ){
					$k = $k + 1;
				}
			}
			if ($k <= 0 ){return false;	}else{ return true;	}	
		}
	}
}
function onegate_is_doc_with_multi_object_for_recordtype($p_recordtype_id, $p_arr_all_recordtype){
	if(sizeof($p_arr_all_recordtype) <= 0){
		return false;
	}else{
		if ($p_recordtype_id != ""){
			for($i=0; $i< sizeof($p_arr_all_recordtype); $i++){
				if (($p_arr_all_recordtype[$i]['C_DOC_WITH_MULTI_OBJECT'] == 1) && ($p_arr_all_recordtype[$i]['PK_RECORDTYPE'] == $p_recordtype_id)){
					return true;
				}
			}
		}
	}
	return false;
}
function onegate_is_handover_to_customer_for_recordtype($p_recordtype_id, $p_arr_all_recordtype){
	if(sizeof($p_arr_all_recordtype) <= 0){
		return false;
	}else{
		if ($p_recordtype_id != ""){
			for($i=0; $i< sizeof($p_arr_all_recordtype); $i++){
				if (($p_arr_all_recordtype[$i]['C_HANDOVER_TO_CUSTOMER'] == 1) && ($p_arr_all_recordtype[$i]['PK_RECORDTYPE'] == $p_recordtype_id)){
					if($p_arr_all_recordtype[$i]['C_RESULT_DOC_TYPE'] != "KHAC"){
						return true;
					}
				}
			}
		}
	}
	return false;
}
function onegate_is_manage_on_post_liciense_for_recordtype($p_recordtype_id, $p_arr_all_recordtype){
	if(sizeof($p_arr_all_recordtype) <= 0){
		return false;
	}else{
		if ($p_recordtype_id != ""){
			for($i=0; $i< sizeof($p_arr_all_recordtype); $i++){
				if (($p_arr_all_recordtype[$i]['C_MANAGE_ON_POST_LICIENSE'] == 1) && ($p_arr_all_recordtype[$i]['PK_RECORDTYPE'] == $p_recordtype_id)){
					return true;
				}
			}
		}
	}
	return false;
}
//Ham tra lai cac label tuong ung voi: trinh ky, da cap phep, tu choi cap phep
function onegate_get_label_of_result_type($p_arr_all_recordtype,$p_recordtype_id){
	global $arr_result_doc_type;
	if(sizeof($p_arr_all_recordtype) <= 0){
		return false;
	}else{
		$arr_result_label = array();
		$v_found = false;
		$v_result_type='';
		for($i=0; $i< sizeof($p_arr_all_recordtype); $i++){
			if ($p_arr_all_recordtype[$i]['PK_RECORDTYPE'] == $p_recordtype_id){
				$v_found = true;
				$v_result_type = $p_arr_all_recordtype[$i]['C_RESULT_DOC_TYPE'];
				break;
			}
		}
		if ($v_found==false){
			return false;
		}
		for($k=0; $k<sizeof($arr_result_doc_type); $k++){
			if ($arr_result_doc_type[$k][0] == $v_result_type){
				$arr_result_label[0] = $arr_result_doc_type[$k][2];
				$arr_result_label[1] = $arr_result_doc_type[$k][3];
				$arr_result_label[2] = $arr_result_doc_type[$k][4];
				$arr_result_label[3] = $arr_result_doc_type[$k][5];
				$arr_result_label[4] = $arr_result_doc_type[$k][6];
				$arr_result_label[5] = $arr_result_doc_type[$k][7];
				$arr_result_label[6] = $arr_result_doc_type[$k][8];
				$arr_result_label[7] = $arr_result_doc_type[$k][9];
				break;
			}
		}	
		if (sizeof($arr_result_label)<=0){
			$arr_result_label[0] = "Tr&#297;nh l&#227;nh &#273;&#7841;o k&#253; gi&#7843;i quy&#7871;t";
			$arr_result_label[1] = "&#272;&#227; gi&#7843;i quy&#7871;t";
			$arr_result_label[2] = "Kh&#244;ng gi&#7843;i quy&#7871;t";
			$arr_result_label[3] = "N&#7897;i dung v&#259;n b&#7843;n tr&#7843; k&#7871;t qu&#7843;";
			$arr_result_label[4] = "In v&#259;n b&#7843;n tr&#7843; k&#7871;t qu&#7843;";
			$arr_result_label[5] = "C&#7853;p nh&#7853;t d&#7919; li&#7879;u sau x&#7917; l&#253;";
			$arr_result_label[6] = "V&#259;n b&#7843;n gi&#7843;i quy&#7871;t &#273;&#227; h&#7871;t hi&#7879;u l&#7921;c";
			$arr_result_label[7] = "Chuy&#7875;n c&#417; quan c&#243; th&#7849;m quy&#7873;n gi&#7843;i quy&#7871;t";
		}
	}
	return $arr_result_label;
}
//Ham nay tr ve ID cua phong ban thu ly ho so 
function onegate_get_handle_agency_id($p_arr_all_recordtype,$p_recordtype_id){
	$v_handle_agency_id = '';
	if(sizeof($p_arr_all_recordtype) > 0){
		if ($p_recordtype_id !=""){
			for($i=0; $i< sizeof($p_arr_all_recordtype); $i++){
				if ($p_arr_all_recordtype[$i]['PK_RECORDTYPE'] == $p_recordtype_id ){
					$v_handle_agency_id = $p_arr_all_recordtype[$i]['FK_HANDLE_AGENCY'];
				}
			}
		}
	}
	return $v_handle_agency_id;
}
function display_type($v_handle_id,$v_approve_id,$v_flag){
	$v_staff_id = $_SESSION['staff_id'];
	$v_type = -1;
	//NSD la truong phong phu trach thu ly, nhung khong co quyen ky cap phep
	if (($v_staff_id==$v_handle_id)&&($v_flag==30)&&($v_staff_id!=$v_approve_id)){
		$v_type = 0;
	}
	//NSD la truong phong phu trach thu ly, co quyen ky cap phep
	if (($v_staff_id==$v_handle_id)&&($v_flag==30)&&($v_staff_id==$v_approve_id)){
		$v_type = 1;
	}
	//NSD khong la truong phong thu ly, nhung la nguoi co quyen ky cap phep
	if (($v_staff_id!=$v_handle_id)&&($v_flag==40)&&($v_staff_id==$v_approve_id)){
		$v_type = 2;
	}
	return $v_type;
}
/*Ham nay co chuc nang phan trang tren form hien thi danh sach 
  --$totalRows: Tong so phan tu cua danh sach
  --$currentPage: Trang hien tai
  --$numberPage: So trang hien thi
  --$numberRowsofPage:So phan tu cua moi trang
*/
function PageBreak($totalRows,$currentPage,$numberPage,$numberRowsofPage){			 				
	$ret_str="";	         		 
	$maxRow = $numberRowsofPage;
	$maxPage = $numberPage;
	if($totalRows%$maxRow==0){ 
		$totalPages = (int)($totalRows/$maxRow);
	}else{
		$totalPages = (int)($totalRows/$maxRow+1);
	}	
	$curRow = ($currentPage-1)*$maxRow+1;
	//$ret_str ="Trang :&nbsp;".$currentPage;
	$ret_str ="T&#7893;ng s&#7889; trang:&nbsp;";
	$ret_str .=$totalPages; 
	if($totalRows>$maxRow){
		$start=1;
		$end=1;
		$ret_temp ="";				 	 
		for($i=1;$i<=$totalPages;$i++){	
			if(($i>((int)(($currentPage-1)/$maxPage))* $maxPage) && ($i<=((int)(($currentPage-1)/$maxPage+1))* $maxPage)){
				if($start==1){
					$start=$i;
				}
				if($i==$currentPage){      
					$ret_temp .= "<B style='cursor:default'>".$i."</B>&nbsp;";
				}else{
					$ret_temp .= "<a href='javascript:page_onclick(".$i.");'>".$i;
					$ret_temp .="</a>&nbsp;";	
				}
				$end=$i;	
			}
		}//End for
		$ret_str.= "&nbsp;&nbsp;Trang:&nbsp;" ;
		if($currentPage>$maxPage){
			$ret_str .="<a href='javascript:page_onclick(".($start-1).");'>";
			$ret_str .="<B>Tr&#432;&#7899;c</B></a>&nbsp;";
		}
		$ret_str.=$ret_temp;
		//Lay ra doan hien tai
		$currentSegment  = (int)(($currentPage-1)/$maxPage) + 1;
		
		if(($currentSegment*$maxPage) < $totalPages){
			$ret_str .="<a href='javascript:page_onclick(".($end+1).");'>";
			$ret_str .="<B>Ti&#7871;p</B></a>&nbsp;";
		}
	}//End If
	return $ret_str;
}
function FirstStringToLower($p_str){
	$v_temp = substr($p_str,1,strlen($p_str));
	$v_temp = strtolower(substr($p_str,0,1)).$v_temp ;
	return $v_temp;
}
function create_image($string,$size,$file_name){ 
	$width = 11*$size; 
	$height = 23; 		
	$im = imagecreate($width, $height); 
	$bg = imagecolorallocate($im, 255, 255, 255); 
	$black = imagecolorallocate($im, 0, 0, 0); 
	$grey = imagecolorallocate($im, 170, 170, 170); 
	imagerectangle($im,0, 0, $width-1, $height-1, $grey); 
	imagestring($im, 5, $size, 5, $string, $black); 
	imagejpeg($im,$file_name); 
	imagedestroy($im); 
}
function random_string($size = 5){
   $possible = "0123456789QWERTYUIOPLKJHGFDSAZXCVBNM";
   $string = "";
   while ($i < $size) {

     $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
     if (!strstr($password, $char)) {
       $password .= $char;
       $i++;
     }

   }
  return $password;
}

function show_info_by_citizen($p_url_frondend) {
	global $_ISA_CAM_SITE_PATH;
	$enduser_label = "<b>Th&#244;ng tin c&#244;ng d&#226;n</b>";
	$html_str = '<table>';
	if (isset($_SESSION['citizen_id'])){
        $citizen_name = $_SESSION['citizen_fullname'];
	    $html_str = $html_str . '<tr><td class="normal_link" align="center">'. $enduser_label.'</td>';
		$html_str = $html_str . '<tr><td colspan="10" align="center" class="normal_link">';
		$html_str = $html_str . '<a href="javascript:goto_url(&quot;'.$_ISA_CAM_SITE_PATH.'citizen/index.php?fuseaction=DISPLAY_SINGLE_CITIZEN&url_back='.$p_url_frondend.'&quot;,0)" target="_parent">' . $citizen_name . '</a></td></tr>';
		$html_str = $html_str . '<tr><td colspan="10" align="center" class="normal_link"><a href="javascript:goto_url(&quot;'.$_ISA_CAM_SITE_PATH.'login/logout.php?url_back='.$p_url_frondend.'&quot;,0)" target="_parent">Tho&#225;t</a></td></tr>';
	}else{
		$html_str = $html_str . '<tr><td colspan="10" align="center" class="normal_link"><b>D&#224;nh cho c&#244;ng d&#226;n</b></td></tr>';
		$html_str = $html_str . '<tr><td colspan="10" align="center" class="normal_link">';
		$html_str = $html_str . '<a href="javascript:goto_url(&quot;'.$_ISA_CAM_SITE_PATH.'login/index.php?url_back='.$p_url_frondend.'&quot;,0)" target="_parent">&#272;&#259;ng nh&#7853;p</a>';
		$html_str = $html_str . '</td></tr>';
				
		$html_str = $html_str . '<tr><td colspan="10" align="center" class="normal_link">';
		$html_str = $html_str . '<a href="javascript:goto_url(&quot;'.$_ISA_CAM_SITE_PATH.'citizen/index.php?fuseaction=DISPLAY_SINGLE_CITIZEN&url_back='.$p_url_frondend.'&quot;,0)" target="_parent">&#272;&#259;ng k&#253; t&#224;i kho&#7843;n</a>';
		$html_str = $html_str . '</td></tr>';
		
		$html_str = $html_str . '<tr><td>&nbsp;</td></tr>';
	}
	$html_str = $html_str . '</table>';
	return $html_str;
}
function init_register_infomation($v_xml_string){
	if (!isset($_SESSION['arr_citizen']) || is_null($_SESSION['arr_citizen'])){
		return '<?xml version="1.0" encoding="UTF-8"?><root><data_list></data_list></root>';
	}
	foreach($_SESSION['arr_citizen'] as $v_arr_citizen) {
		$arr_citizen = $v_arr_citizen;
	}
	$v_xml_register = base64_decode($arr_citizen['detail_info']);
	$v_xml_string_ret = '';
	$v_replace_rax = new RAX(); 
	$v_replace_rec = new RAX(); 
	$v_replace_rax->open($v_xml_string);
	$v_replace_rax->record_delim = 'replace';
	$v_replace_rax->parse();
	$v_replace_rec = $v_replace_rax->readRecord(); 
	while ($v_replace_rec) { 
		$v_replace_row = $v_replace_rec->getRow();
		$v_tag_in_form = $v_replace_row["tag_in_form"];
		$v_tag_in_citizen = $v_replace_row["tag_in_citizen"];
		
		if ($v_xml_register!=""){
			$v_register_rax = new RAX(); 
			$v_register_rec = new RAX();
			$v_register_rax->open($v_xml_register);
			$v_register_rax->record_delim = 'data_list';
			$v_register_rax->parse();
			$v_register_rec = $v_register_rax->readRecord(); 
			$v_register_row = $v_register_rec->getRow();
			$v_citizen_value = $v_register_row[$v_tag_in_citizen];		
		}
	
		$v_xml_string_ret .= '<'.$v_tag_in_form.'>'.$v_citizen_value.'</'.$v_tag_in_form.'>';

		$v_replace_rec = $v_replace_rax->readRecord();
	}
	$v_xml_string_ret = '<?xml version="1.0" encoding="UTF-8"?><root><data_list>'.$v_xml_string_ret;
	$v_xml_string_ret .= '</data_list></root>';
	//echo htmlspecialchars($v_xml_string_ret);
	
	return  $v_xml_string_ret;
}

// Ham _XML_get_xml_tag_value tra lai GIA TRI cua mot the XML voi dieu kien the do bat buoc phai co trong chuoi XML
// $p_xml_string: Chuoi  XML
// $p_xml_tag: ten the XML can lay gia tri
// 
function _XML_get_xml_tag_value($p_xml_string,$p_xml_tag){
	$array = explode('<'.$p_xml_tag.'>', $p_xml_string);
	$p_xml_string = $array[1];
	$array = explode('</'.$p_xml_tag.'>', $p_xml_string);
	$p_xml_string = $array[0];
	return $p_xml_string;
}

function _replace_xml_tag_value($p_xml_string,$p_xml_tag,$p_xml_value){
	$first_array = explode('<'.$p_xml_tag.'>', $p_xml_string);
	$last_array = explode('</'.$p_xml_tag.'>', $first_array[1]);
	$p_xml_string = $first_array[0].'<'.$p_xml_tag.'>'.$p_xml_value .'</'.$p_xml_tag.'>'.$last_array[1];
	return $p_xml_string;
}

function _insert_xml_tag_and_value($p_xml_string,$p_xml_tag,$p_xml_value){
	
	$v_xml_str_header = '<?xml version="1.0" encoding="UTF-8"?><root><data_list>';
	//Cong them gia tri cua xau chua so giay phep vao
	$v_xml_replace = $v_xml_str_header . '<'.$p_xml_tag.'>'.$p_xml_value.'</'.$p_xml_tag.'>';
	$p_xml_string = str_replace($v_xml_str_header,$v_xml_replace,$p_xml_string);
	return $p_xml_string;
}
function onegate_get_check_recordtype($p_arr_all_recordtype,$p_recordtype_id){
	for($i=0,$n=sizeof($p_arr_all_recordtype); $i< $n; $i++){
		if ($p_arr_all_recordtype[$i]['PK_RECORDTYPE'] == $p_recordtype_id){
			$v_handle_leader = $p_arr_all_recordtype[$i]['FK_HANDLE_LEADER'];
			$v_approve_leader = $p_arr_all_recordtype[$i]['FK_APPROVE_LEADER'];
		}
	}
	if((trim($v_handle_leader) == "0")&&(trim($v_approve_leader) == "0" )){
		return false;
	}else{
		return true;
	}

}
///////////////////////
function get_all_leader_by_code($arr_code){
	$i=0;
	$arr_list_code = split(",",$arr_code);	
	$v_count = sizeof($arr_list_code);	
	foreach($_SESSION['arr_all_staff'] as $staff_item) {
		$v_position_group = $staff_item['position_group_code'];
		for($v_index = 0;$v_index< $v_count;$v_index++){
			if($v_position_group == $arr_list_code[$v_index]){
				$arr_return[$i][0]=$staff_item['id'];
				$arr_return[$i][1]=$staff_item['position_name'];
				$arr_return[$i][2]=$staff_item['code'];
				$arr_return[$i][3]=$staff_item['name'];
				$arr_return[$i][4]=1;
				$arr_return[$i][5]=1;
				$i++;
			}	
		}	
	}
	return $arr_return;
}
/**
 * Chuc nang: Phan trang tren danh sach voi Selectbox
 * Nguoi tao: HUNGVM
 * Ngay tao: 08/02/2007
 * @param $p_total_record:	Tong so ban ghi 
 * @param $p_current_page:	Trang hien tai
 * @param $p_number_record_per_page: So ban ghi tren 1 trang
 * @return Chuoi HTML de sinh ra selectbox phan trang
 */
function onegate_pagebreak_selectbox($p_total_record,$p_current_page,$p_number_record_per_page,$v_fuseaction){
	$v_html_string = "";
	if($p_total_record%$p_number_record_per_page==0){
		$v_number_page = (int)($p_total_record/$p_number_record_per_page);
	}else{
		$v_number_page = (int)($p_total_record/$p_number_record_per_page)+1;		
	}
	$v_html_string = $v_html_string . "T&#7893;ng s&#7889;&nbsp;" . $v_number_page . "&nbsp;trang.&nbsp;&nbsp;Xem &nbsp;" . "<select class='normal_selectbox' name='sel_page' style='width:auto' title='Ch&#7885;n trang mu&#7889;n xem' style='width:40'" . "onChange='page_onchange(this,&quot;" . $v_fuseaction ."&quot;)'" . "onKeyDown='change_focus(document.forms(0),this)' >";
	for ($i=1; $i<=$v_number_page;$i++){				
		if ($p_current_page == $i){
			$v_select = " selected ";
		}else{
			$v_select = " ";
		}
		$v_html_string = $v_html_string . "<option id='' value='$i' name='$i' $v_select> " . "Trang &nbsp;" . $i  . "</option>";
	}
	$v_html_string = $v_html_string . "</select>";
	return $v_html_string;
}
function onegate_change_record_number_page($v_value,$v_fuseaction){
	$v_html_string = "";
	$v_sql = "ONEGATE_ListGetAllbyCode " . "SO_HS_TREN_TRANG";
	$v_html_string = $v_html_string . "Hi&#7875;n th&#7883; ";
	$v_html_string = $v_html_string . "<select class='normal_selectbox' name='cbo_nuber_record_page' style='width:60' onChange='page_record_number_onchange(this,&quot;" . $v_fuseaction ."&quot;)'" . "onKeyDown='change_focus(document.forms(0),this)'>";
	$v_html_string = $v_html_string . _generate_select_option(Query_Data($v_sql),0,0,1,$v_value);
	$v_html_string = $v_html_string . "</select>";	
	$v_html_string = $v_html_string . "&nbsp;h&#7891; s&#417;/1 trang";
	return $v_html_string;
}
?>